英文:
replace blanks in the column with the values in the same column in DF based on the corresponding values in another column in pandas
问题
初始表格:
| Acc num | Bank name |
| 100250-- | NaN |
| 100250 | NaN |
| 100250 | NaN |
| 100250 | SBI |
| 200100 | NaN |
| 200100 | NaN |
| 200100 | ICICI |
| 200100 | NaN |
期望输出(根据pandas中另一列的相应值填充列中的空白值):
| Acc num | Bank name |
| 100250-- | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
英文:
Initial table:
| Acc num | Bank name |
| 100250-- | NaN |
| 100250 | NaN |
| 100250 | NaN |
| 100250 | SBI |
| 200100 | NaN |
| 200100 | NaN |
| 200100 | ICICI |
| 200100 | NaN |
Desired output (fill the blanks in the column with the values in the same column in DF based on the corresponding values in another column in pandas):
| Acc num | Bank name |
| 100250-- | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
答案1
得分: 0
你可以在df
的Acc num
列上使用groupby
,然后使用bfill
和ffill
来填充空白值,将它们填充为上方和下方的数值:
df['Bank name'] = df.groupby('Acc num')['Bank name'].bfill().ffill()
输出结果:
| Acc num | Bank name |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
英文:
You can use groupby
on df
on column Acc num
, and then use bfill
and ffill
to fill the blank values with values above and below:
df['Bank name'] = df.groupby('Acc num')['Bank name'].bfill().ffill()
Output:
| Acc num | Bank name |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 100250 | SBI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
| 200100 | ICICI |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论