如何用最高值替换熊猫中特定列的连续零值
假设我有这样的数据框架,
import pandas as pd
data = {'first_column': ['A', 'null', 'null', 'B', 'null', 'null', 'null' ],
'second_column': [1, 3, 5, 32, 32, 12, 51]}
df = pd.DataFrame(data)
print (df)
我想生成这个
data = {'first_column': ['A', 'A', 'A', 'B', 'B', 'B', 'B' ],
'second_column': [1, 3, 5, 32, 32, 12, 51]}
df = pd.DataFrame(data)
print (df)
问题?我是新手,我知道替换。
Suppose I have a data frame like this
import pandas as pd
data = {'first_column': ['A', 'null', 'null', 'B', 'null', 'null', 'null' ],
'second_column': [1, 3, 5, 32, 32, 12, 51]}
df = pd.DataFrame(data)
print (df)
I want to produce this
data = {'first_column': ['A', 'A', 'A', 'B', 'B', 'B', 'B' ],
'second_column': [1, 3, 5, 32, 32, 12, 51]}
df = pd.DataFrame(data)
print (df)
how do I do it? I am newbie, I know replace.na, but it's not exactly straight forward I can apply here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将“ null”掩盖为null/nan vaues,然后用
ffill
向前填充:Mask the 'null' as null/nan vaues, then forward fill with
ffill
:如果您的值实际上是Na而不是字符串“ null”,则PANDAS具有
.fillna()
函数,您可以使用。文档If your values are actually na as opposed to the string 'null' then Pandas has a
.fillna()
function you can use. Documentation here.您可以替换
'null'
nan 的字符串,然后使用fillna()
:output:output:output:output:
You can replace the
'null'
string byNaN
and then usefillna()
:Output: