Collapsing rows in a Pandas dataframe if all rows have only one value in their columns

前端 未结 2 999
太阳男子
太阳男子 2020-12-03 23:50

I have following DF

         col1  |  col2   | col3   | col4   | col5  | col6
    0    -     |   15.0  |  -     |  -     |   -   |  -
    1    -     |   -            


        
2条回答
  •  不知归路
    2020-12-04 00:07

    You can use max, but you need to convert the null values in the string-valued columsn (which is a bit ugly unfortunately)

    >>> df = pd.DataFrame({'col1':[np.nan, "ABC1"], 'col2':[15.0, np.nan]})
    
    >>> df.apply(lambda c: c.fillna('') if c.dtype is np.dtype('O') else c).max()
    col1    ABC1
    col2      15
    dtype: object
    

    You could also you a combination of backfill and forwardfill to fill in the gaps, this could be useful if only want to apply this to some of your columns:

    >>> df.apply(lambda c: c.fillna(method='bfill').fillna(method='ffill'))
    

提交回复
热议问题