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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With