Chris L
Chris L

Reputation: 329

Merging non NaN values values only

I have a table which only includes one value per row and then repeats the same index for each variable. Here is the code and the df output.

import pandas as pd
import numpy as np
df=pd.DataFrame([[1.0,np.NaN,np.NaN,np.NaN],[np.NaN,6.0,np.NaN,np.NaN],[np.NaN,np.NaN,11.0,np.NaN],[np.NaN,np.NaN,np.NaN,16]],columns=['A','B','C','D'])
df.index=['01-Jan-2021','01-Jan-2021','01-Jan-2021','01-Jan-2021']

Date        A    B      C     D
01-Jan-2021 1.0  NaN    NaN   NaN
01-Jan-2021 NaN  6.0    NaN   NaN
01-Jan-2021 NaN  NaN    11.0  NaN
01-Jan-2021 NaN  NaN    NaN   16.0

What I am trying to figure out is how to merge and get this result...

Date        A    B     C     D
1-Jan-2021  1.0  6.0   11.0  16.0

Upvotes: 0

Views: 219

Answers (1)

jezrael
jezrael

Reputation: 862511

If need first non missing value use GroupBy.first:

df.groupby('Date').first()

If there is only one value per column and group same ouput is if aggrregate mean, max, min, sum:

df.groupby('Date').sum()

df.sum(level='Date')

Upvotes: 4

Related Questions