Selective Pandas Dropna So That The Dataframe Will Be Different Lengths
I am attempting to drop the nan values in my DataFrame df, however I am having difficulty in dropping the for each column without effecting the entire row. An example of my df can
Solution 1:
Use justify with DataFrame.dropna
:
df = pd.DataFrame(justify(df.values, invalid_val=np.nan, axis=0, side='up'),
index=df.index,
columns=df.columns).dropna(how='all')
print (df)
Advertising No Advertising
0 71.0 7.0
1 65.0 36.0
2 14.0 9.0
3 76.0 103.0
4 73.0 NaN
5 85.0 NaN
6 17.0 NaN
Another slowier solution is use DataFrame.apply
with Series.dropna
:
df = df.apply(lambda x: pd.Series(x.dropna().values))
print (df)
Advertising No Advertising
0 71.0 7.0
1 65.0 36.0
2 14.0 9.0
3 76.0 103.0
4 73.0 NaN
5 85.0 NaN
6 17.0 NaN
Mixing numeric with strings (empty strings) is not good idea, because if need processes number later pandas functions failed, so rather dont do it.
But is is possible by :
df = df.fillna('')
Post a Comment for "Selective Pandas Dropna So That The Dataframe Will Be Different Lengths"