2

I am having below data in pandas dataframe. Some date vales are repeating(2010-07-31,2010-10-31). Need to remove repeated dates and merge the values between two rows.

I have used the solution from the following question. But using below code sets Date column as Index. I don't want date column to be set as Index. Need help in removing duplicate dates and merging values of two rows in Pandas Dataframe (python)

Code:

  df = df.groupby(['Date']).sum()

Dataframe df:

 Index     Date                A       B           C       D
  1     2010-06-30 0:00:00  47.1    29.34       0.036   100.8   
  2     2010-07-31 0:00:00  47.1    29.34               
  3     2010-07-31 0:00:00                      -4.644  100.2   
  4     2010-08-31 0:00:00  47.1    29.34       -1.481  100.4   
  5     2010-09-30 0:00:00  29.3    14.15        3.865  101.6   
  6     2010-10-31 0:00:00  29.3    14.15               
  7     2010-10-31 0:00:00                       0.517  102.6 

Expected output:

 Index     Date              A       B           C       D
  1   2010-06-30 0:00:00   47.1    29.34       0.036   100.8   
  2   2010-07-31 0:00:00   47.1    29.34       -4.644  100.2           
  4   2010-08-31 0:00:00   47.1    29.34       -1.481  100.4   
  5   2010-09-30 0:00:00   29.3    14.15        3.865  101.6   
  6   2010-10-31 0:00:00   29.3    14.15        0.517  102.6   
3
  • 1
    Try this: df.groupby(['Date'], as_index=False).sum() Commented Nov 1, 2017 at 20:39
  • It works fine. Thanks a lot! Commented Nov 1, 2017 at 20:47
  • 1
    @Reese do not include answer in the comment , you can add it as an answer Commented Nov 1, 2017 at 20:50

1 Answer 1

2

As Reese mentioned -

df.groupby(['Date'], as_index=False).sum() will not set date column as Index

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.