0

I am following data in pandas dataframe. Some date vales are repeating(2010-07-31,2010-10-31). How to remove repeated dates and merge the values between two rows. Take A % B values from 1st row and C & D values from 2nd row.

        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:

        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       

Thanks!

2 Answers 2

3

Use pandas groupby and aggregate it by sum (use Dated as column name, instead of the reserved Date):

df.groupby(['Dated']).sum()

enter image description here

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

Comments

2

Magic of stack and unstack

df.set_index('Date').replace({'':np.nan}).stack().unstack()
Out[515]: 
                      A      B      C      D
Date                                        
2010-06-300:00:00  47.1  29.34  0.036  100.8
2010-07-310:00:00  47.1  29.34 -4.644  100.2
2010-08-310:00:00  47.1  29.34 -1.481  100.4
2010-09-300:00:00  29.3  14.15  3.865  101.6
2010-10-310:00:00  29.3  14.15  0.517  102.6

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.