如何用最高值替换熊猫中特定列的连续零值

发布于 2025-01-22 07:19:04 字数 441 浏览 3 评论 0原文

假设我有这样的数据框架,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2025-01-29 07:19:04

将“ null”掩盖为null/nan vaues,然后用ffill向前填充:

df['first_column'] = df['first_column'].mask(df['first_column'] == 'null').ffill()

Mask the 'null' as null/nan vaues, then forward fill with ffill:

df['first_column'] = df['first_column'].mask(df['first_column'] == 'null').ffill()
べ映画 2025-01-29 07:19:04

如果您的值实际上是Na而不是字符串“ null”,则PANDAS具有.fillna()函数,您可以使用。文档

df['first_column'] = df['first_column'].fillna(method='ffill')

If your values are actually na as opposed to the string 'null' then Pandas has a .fillna() function you can use. Documentation here.

df['first_column'] = df['first_column'].fillna(method='ffill')
满意归宿 2025-01-29 07:19:04

您可以替换'null' nan 的字符串,然后使用 fillna() :output:output:output:

df['first_column'] = df['first_column'].replace('null', pd.NA).fillna(method='ffill')
# But if there are actually null values instead of 'null' then use:
# df['first_column'] = df['first_column'].fillna(method='ffill')

output:

  first_column  second_column
0            A              1
1            A              3
2            A              5
3            B             32
4            B             32
5            B             12
6            B             51

You can replace the 'null' string by NaN and then use fillna():

df['first_column'] = df['first_column'].replace('null', pd.NA).fillna(method='ffill')
# But if there are actually null values instead of 'null' then use:
# df['first_column'] = df['first_column'].fillna(method='ffill')

Output:

  first_column  second_column
0            A              1
1            A              3
2            A              5
3            B             32
4            B             32
5            B             12
6            B             51
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文