Nai J.
Nai J.

Reputation: 1

Date in a column as index

I used groupby for to calculate mean every day during a year but I got a dataframe with two columns(TIME) as index the firs indicate the month with one number and the second the day, and I would like to get one column as index with the month and day:

TIME TIME   A  B 
  1    1    3  4
       2    4  5 
       3    2  1

OUT:

TIME  A B
01-1  3 4
01-2  4 5
01-3  2 1

Upvotes: 0

Views: 26

Answers (1)

Akhilesh_IN
Akhilesh_IN

Reputation: 1327

as index has two TIME columns it is raising an error when reset_index , renaming solves problems

In [41]: dfs
Out[41]:
           A  B
TIME TIME
1    1     3  4
     2     4  5
     3     2  1

In [42]: dfs.index.names = ['M','TIME']

In [43]: df=dfs.reset_index()

In [44]: df['TIME'] = df['M'].astype(str) + "-" + df['TIME'].astype(str)

In [45]: df[['TIME','A','B']]
Out[45]:
  TIME  A  B
0  1-1  3  4
1  1-2  4  5
2  1-3  2  1

Upvotes: 0

Related Questions