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.py275 chars9 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.py196 chars8 lines
Where the a
, b
, c
, d
, e
, and f
are the index labels.
gistlibby LogSnag