Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas : Convert multiple rows into single row, ignoring NaN's

I have a DataFrame similar to the one mentioned below,

 Age    Sex    Name ....
 12     NaN    NaN
 NaN    Male   NaN
 NaN    NaN    David

I want to convert it into a dataframe with one row, ignoring the NaN's and merging them

 Age    Sex    Name
 12     Male   David

How to do this is Pandas?

like image 777
Avis Avatar asked Jun 16 '16 12:06

Avis


2 Answers

You can use pd.concat to combine all columns after .dropna() and .reset_index() like so:

pd.concat([df[col].dropna().reset_index(drop=True) for col in df], axis=1)

to get:

    Age   Sex   Name
0  12.0  Male  David
like image 160
Stefan Avatar answered Sep 25 '22 06:09

Stefan


Another method is to apply a lambda that calls first_valid_index to return the first valid row value:

In [246]:
df.apply(lambda x: pd.Series(x[x.first_valid_index()]))

Out[246]:
    Age   Sex   Name
0  12.0  Male  David
like image 43
EdChum Avatar answered Sep 22 '22 06:09

EdChum