Andrew
Andrew

Reputation: 1126

No index in creating dataframe

I create a small dataframe by these lines:

import pandas as pd
 
my_funds = [1, 2, 5, 7, 9, 11]
my_time = ['2020-01', '2019-12', '2019-11', '2019-10', '2019-09', '2019-08']
df = pd.DataFrame({'TIME': my_time, 'FUNDS':my_funds})

df

The output is then:

    TIME    FUNDS
0   2020-01     1
1   2019-12     2
2   2019-11     5
3   2019-10     7
4   2019-09     9
5   2019-08     11

Would it be possible to modify the code in order to create the dataframe without an index ? I was even not able... So, thanks for any hint.

Upvotes: 1

Views: 16230

Answers (1)

Josie
Josie

Reputation: 175

A Pandas DataFrame needs an index, but you can set it to an existing column. If all your TIME entries are unique, you can do

import pandas as pd
 
my_funds = [1, 2, 5, 7, 9, 11]
my_time = ['2020-01', '2019-12', '2019-11', '2019-10', '2019-09', '2019-08']
df = pd.DataFrame({'FUNDS': my_funds}, index=my_time)

df

gives you

         FUNDS
2020-01      1
2019-12      2
2019-11      5
2019-10      7
2019-09      9
2019-08     11

Upvotes: 5

Related Questions