[英]Replace missing value in a row if there's a match in two columns from another row using Pandas
[英]Replace missing value from another row in pandas
您需要groupby
與ffill
和bfill
themes.groupby('code').apply(lambda x : x.ffill().bfill())
一種方法是在刪除空值后創建序列。
然后將pd.Series.fillna
與pd.Series.map
pd.Series.fillna
使用:
df = pd.DataFrame({'code': [1, 2, 3, 1, 2, 4],
'name': ['A', np.nan, 'C', np.nan, 'B', 'D']})
s = df.set_index('code')['name'].dropna()
df['name'] = df['name'].fillna(df['code'].map(s))
print(df)
code name
0 1 A
1 2 B
2 3 C
3 1 A
4 2 B
5 4 D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.