instantiate a pd dataframe with an index in python

To instantiate a pandas DataFrame with an index in Python, you can pass a dictionary to the pd.DataFrame() function in the following way:

main.py
import pandas as pd

data = {'country': ['USA', 'Canada', 'Mexico', 'USA', 'Canada', 'Mexico'],
        'year': [2018, 2018, 2018, 2019, 2019, 2019],
        'population': [327, 37, 129, 330, 38, 131]}

df = pd.DataFrame(data, index=['a', 'b', 'c', 'd', 'e', 'f'])
print(df)
275 chars
9 lines

In this example, we created a dictionary data with three keys: 'country', 'year', and 'population'. Each key corresponds to a list of data that we want to populate in the DataFrame. The index parameter is used to set the index of the DataFrame. The result of running this code will be:

main.py
  country  year  population
a     USA  2018         327
b  Canada  2018          37
c  Mexico  2018         129
d     USA  2019         330
e  Canada  2019          38
f  Mexico  2019         131
196 chars
8 lines

Where the a, b, c, d, e, and f are the index labels.

related categories

gistlibby LogSnag