Below code creates a random pandas dataframe with columns with all the data types.
specify n=# for required sample size.
This first creates a dictionary ddict which is supplied as 'data' to dataframe.
This uses list comprehensions for creating ID and names variables.
Output
ID | name | order | items | age | x | date | |
---|---|---|---|---|---|---|---|
0 | yFUiR | ryrafuhjx | 0 | 2 | 27 | -3.29647 | 1/31/2022 |
1 | 9nqMw | bpnlwpjhw | 1 | 0 | 33 | -0.27372 | 2/28/2022 |
2 | QX1Mf | rxxmtpkhl | 2 | 4 | 22 | -0.51878 | 3/31/2022 |
3 | izsaB | ygezcbhga | 3 | 1 | 23 | -0.40394 | 4/30/2022 |
4 | 19NeT | xhydvnkko | 4 | 4 | 24 | 0.223726 | 5/31/2022 |
5 | JPrCT | pgeqjgdxz | 5 | 0 | 31 | -0.42457 | 6/30/2022 |
Output
id | date | group | name | age | type | x | |
---|---|---|---|---|---|---|---|
1 | 1 | 12/1/2020 | A | Q | 19 | type 1 | -0.09466 |
2 | 2 | 1/1/2021 | B | E | 27 | type 2 | 2.018424 |
3 | 3 | 2/1/2021 | A | A | 18 | type 3 | -0.06271 |
4 | 4 | 3/1/2021 | B | Y | 25 | type 4 | 1.30487 |
5 | 5 | 4/1/2021 | A | J | 24 | type 5 | 2.286645 |
6 | 6 | 5/1/2021 | B | D | 21 | type 6 | -1.38886 |
insall a package:
import a package
install a library:
Import a library
To read data from a csv file in current working directory
To read data from a local csv file
Python raw string is created by prefixing a string literal with βrβ or βRβ. Python raw string treats backslash
\
as a literal character. This is useful when we want to have a string that contains backslash and donβt want it to be treated as an escape character.
To read data from a local excel file
Read csv file in an url
Read a sas dataset
When you need to read data from multiple files and have to append all of them horizontally.
index=False is required to remove the index column in output file.
set current working directory
Write string in multiple lines with use of 3 quotes and still read whole string as one:
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list (using loops and conditional statements).
Below is without list comprehension:
Output: ['apple', 'banana', 'mango']
Below is with list comprehension:
Output: ['apple', 'banana', 'mango']
List comprehension with conditional statement:
Output: ['banana', 'cherry', 'kiwi', 'mango']
Example: plotting a distribution of a variable
use as.datatype
to convert to required datatype