Skip to content Skip to sidebar Skip to footer

Python Dataframe Filling NaN Values Using Information From Other Columns

I tried to solve this problem on my own, but I unfortunately haven't made much progress and would really appreciate anyone who can help me out. My current dataframe contains 3 colu

Solution 1:

using fillna inside groupby

df['x3']=df.groupby(['x1','x2'])['x3'].apply(lambda x : x.fillna(x.median()))
df
Out[928]: 
  x1  x2   x3
0  A   1  2.0
1  B   0  2.0
2  A   0  1.0
3  A   1  2.0
4  A   0  1.0
5  B   1  1.0
6  A   1  1.0
7  B   0  2.0
8  B   0  2.0

Post a Comment for "Python Dataframe Filling NaN Values Using Information From Other Columns"