如何在熊猫数据框中填充nan值? 我的数据是这样的
id state zone
xxx AP south
xxx AP
xxx AP
xxx AP
xxx delhi north
xxx delhi
xxx delhi
xxx delhi
xxx delhi
如何基于已经知道zone
仅属于state
的{{1}}列来填充AP
列中的缺失值,如何使用熊猫填充值? / p>
答案 0 :(得分:1)
我认为您需要:
df = df.sort_values(by="state").ffill()
print(df)
答案 1 :(得分:0)
(id,state)
填充zone
的列进行分组df = pd.DataFrame(data={"id":["x","x","x","x"],
"state":["AP","Delhi","AP","Delhi"],
"zone":["sount","north",np.nan,np.nan]})
res = df.sort_values(['id','state','zone'])
res = df.groupby(['id','state'],as_index=False)['zone'].ffill()
print(res)
id state zone
0 x AP sount
1 x Delhi north
2 x AP sount
3 x Delhi north
df['zone'] = df.groupby(['state'],as_index=False)['zone'].transform(lambda x:x.ffill())
print(df)
id state zone
0 x AP sount
1 x Delhi north
2 x AP sount
3 x Delhi north