英文:
How to fill in missing entries by looking at nearest top filled entry in a pandas dataframe?
问题
我有以下的Dataframe
:
index col1
1 "a"
2
3 "b"
4
5
从中,我想生成以下的Dataframe
:
index col1
1 "a"
2 "a"
3 "b"
4 "b"
5 "b"
我尝试复制Dataframe
并进行查找,但没有成功。
英文:
I have the following Dataframe
:
index col1
1 "a"
2
3 "b"
4
5
And from that I want to produce the following Dataframe
:
index col1
1 "a"
2 "a"
3 "b"
4 "b"
5 "b"
I tried duplicating the dataframe and doing lookup, but didn't work.
答案1
得分: 3
使用 Series.replace
来处理缺失值并向前填充顶部的值:
df['col1'] = df['col1'].replace('', np.nan).ffill()
英文:
Use Series.replace
for missing values and forward filling top values:
df['col1'] = df['col1'].replace('', np.nan).ffill()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论