Each key of the dictionary defines the name of the column and the value (list object) for that key defines the column values.
The code above will create a DataFrame object from a dictionary. The result is:
The module numpy is not required, but I used it to show how to specify a NaN value in the list – np.nan. We could as well just specify None in the list, but I wanted to draw your attention to the fact that NaN is available in the numpy module.
Create a DataFrame from a CSV file
Another way to create a DataFrame is to import all data from a CSV (comma separated values) file. The following code will create a DataFrame from a file cars.csv:
import pandas as pd
df = pd.read_csv('/path/to/file/cars.csv')