简体   繁体   中英

Pandas: Collapse many rows into a single row by removing NaN's in a multiindex dataframe

Below is my pivoted df:

Out[1446]: 
            D                            
A         abc                            
C          G2     G3     G4     G1     G5
B uniq                                   
x 1     100.0    NaN    NaN    NaN    NaN
  2       NaN  200.0    NaN    NaN    NaN
  3       NaN    NaN  300.0    NaN    NaN
y 4       NaN    NaN    NaN  200.0    NaN
  5       NaN    NaN    NaN    NaN  100.0

Now, I want to collapse this dataframe. Logic is: Group on B , ignore uniq , I want to have one row in my dataframe.

Expected output:

Out[1446]: 
            D                            
A         abc                            
C          G2     G3     G4     G1     G5
B                                    
x       100.0  200.0  300.0    NaN    NaN
y         NaN    NaN    NaN  200.0  100.0

How can this be achieved?

EDIT:

In [1472]: df = pd.DataFrame({'A':['abc', 'abc', 'abc', 'abc', 'abc'], 'B':['ab', 'bc', 'cd', 'de', 'ef'], 'C':['G1','G1','G2', 'G3', 'G2'], 'D':[1,2,3,4,5]})

In [1473]: df
Out[1473]: 
     A   B   C  D
0  abc  ab  G1  1
1  abc  bc  G1  2
2  abc  cd  G2  3
3  abc  de  G3  4
4  abc  ef  G2  5

In [1474]: df.pivot(index=None, columns=['A', 'B', 'C'])
Out[1474]: 
     D                    
A  abc                    
B   ab   bc   cd   de   ef
C   G1   G1   G2   G3   G2
0  1.0  NaN  NaN  NaN  NaN
1  NaN  2.0  NaN  NaN  NaN
2  NaN  NaN  3.0  NaN  NaN
3  NaN  NaN  NaN  4.0  NaN
4  NaN  NaN  NaN  NaN  5.0

Expected output:

Out[1474]: 
     D                    
A  abc                    
B   ab   bc   cd   de   ef
C   G1   G1   G2   G3   G2
0  1.0  2.0  3.0  4.0  5.0

If there is always one non missing value per groups use GroupBy.first for return first non NaN value per first level of MultiIndex :

df = df.groupby(level=0).first()
print (df)
       D                            
     abc                            
      G2     G3     G4     G1     G5
x  100.0  200.0  300.0    NaN    NaN
y    NaN    NaN    NaN  200.0  100.0

If there is multiple non missing values only first is returned and of all missing values is returned one row:

print (df)
         D                      
       abc                      
        G2     G3     G4  G1  G5
x 1  100.0    NaN    NaN NaN NaN
  2    8.0  200.0    NaN NaN NaN <- multiple values
  3    NaN    NaN  300.0 NaN NaN
y 4    NaN    NaN    NaN NaN NaN  <- all missing values
  5    NaN    NaN    NaN NaN NaN  <- all missing values

df = df.groupby(level=0).first()
print (df)
       D                      
     abc                      
      G2     G3     G4  G1  G5
x  100.0  200.0  300.0 NaN NaN
y    NaN    NaN    NaN NaN NaN

EDIT:

If no MultiIndex then need different solution:

df = df.pivot(index=None, columns=['A', 'B', 'C'])

#no MultiIndex
print (df.index)
Int64Index([0, 1, 2, 3, 4], dtype='int64')



if df.index.nlevels == 1:

    df1 = df.apply(lambda x: pd.Series(x.dropna().to_numpy())).iloc[[0]]
    print (df1)
             D                    
    A  abc                    
    B   ab   bc   cd   de   ef
    C   G1   G1   G2   G3   G2
    0  1.0  2.0  3.0  4.0  5.0

else:
    df1 = df.groupby(level=0).first()
    print (df1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM