我有数据框,即
Input Dataframe
class section sub marks school city
0 I A Eng 80 jghss salem
1 I A Mat 90 jghss salem
2 I A Eng 50 Nan salem
3 III A Eng 80 gphss Nan
4 III A Mat 45 Nan salem
5 III A Eng 40 gphss Nan
6 III A Eng 20 gphss salem
7 III A Mat 55 gphss Nan
当“class”和“section”列中的值匹配时,我需要替换“school”和“city”中的“Nan”。结果的结果假设是,
输入数据框
class section sub marks school city
0 I A Eng 80 jghss salem
1 I A Mat 90 jghss salem
2 I A Eng 50 jghss salem
3 III A Eng 80 gphss salem
4 III A Mat 45 gphss salem
5 III A Eng 40 gphss salem
6 III A Eng 20 gphss salem
7 III A Mat 55 gphss salem
任何人都可以帮我解决这个问题吗?
最佳答案
使用 lambda function
在列表中指定的列中使用 DataFrame.groupby
每组使用向前和向后填充缺失值 - 对于每个组合,每个组都需要相同的值:
cols = ['school','city']
df[cols] = df.groupby(['class','section'])[cols].apply(lambda x: x.ffill().bfill())
print (df)
class section sub marks school city
0 I A Eng 80 jghss salem
1 I A Mat 90 jghss salem
2 I A Eng 50 jghss salem
3 III A Eng 80 gphss salem
4 III A Mat 45 gphss salem
5 III A Eng 40 gphss salem
6 III A Eng 20 gphss salem
7 III A Mat 55 gphss salem
关于python - 当列值匹配时,Pandas Dataframe 从一行中替换 Nan,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55390298/