create a dataframe in python

To create a dataframe in python, we first need to import the pandas library:

main.py
import pandas as pd
20 chars
2 lines

Then, we can create a dataframe using a dictionary, where keys represent column names and values represent the data for each column. For example:

main.py
data = {"Name": ["Alice", "Bob", "Charlie", "David"],
        "Age": [25, 30, 35, 40],
        "Gender": ["F", "M", "M", "M"]}

df = pd.DataFrame(data)
152 chars
6 lines

This creates a dataframe with 3 columns (Name, Age, Gender) and 4 rows. We can print the dataframe using the following code:

main.py
print(df)
10 chars
2 lines

This will output:

main.py
       Name  Age Gender
0     Alice   25      F
1       Bob   30      M
2   Charlie   35      M
3     David   40      M
120 chars
6 lines

gistlibby LogSnag