仅在上一个有效值和下一个有效值的平均值中仅填充熊猫中连续的nan中的最后一个

发布于 2025-02-03 17:31:37 字数 475 浏览 4 评论 0原文

仅在上一个有效值和下一个有效值的平均值中,仅在熊猫中连续的NAN中填写最后一个。如果一个nan,请填充下一个和上一个的平均值。如果连续两个NAN,则将第二个为下一个和先前的有效值的平均值。

系列:

预期输出:

”在此处输入图像说明”

Fill only last among of consecutive NaN in Pandas by mean of previous and next valid values. If one NaN, then fill with mean of next and previous. If two consecutive NaN, impute second one with mean of next and previous valid values.

Series:

enter image description here

expected output:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉南桥 2025-02-10 17:31:37

想法是删除连续的缺少值而无需最后的值,然后使用interpaly,然后根据条件分配最后的丢失值:

m =  df['header'].isna()
mask = m & ~m.shift(-1, fill_value=False)

df.loc[mask, 'header'] = df.loc[mask | ~m, 'header'].interpolate()
print (df)
    header
0     10.0
1     20.0
2     20.0
3     20.0
4     30.0
5      NaN
6     35.0
7     40.0
8     10.0
9      NaN
10     NaN
11    30.0
12    50.0

详细信息

print (df.assign(m=m, mask=mask))
    header      m   mask
0     10.0  False  False
1     20.0  False  False
2     20.0   True   True
3     20.0  False  False
4     30.0  False  False
5      NaN   True  False
6     35.0   True   True
7     40.0  False  False
8     10.0  False  False
9      NaN   True  False
10     NaN   True  False
11    30.0   True   True
12    50.0  False  False


print (df.loc[mask | ~m, 'header'])
0     10.0
1     20.0
2      NaN
3     20.0
4     30.0
6      NaN
7     40.0
8     10.0
11     NaN
12    50.0
Name: header, dtype: float64

interpaly interpaly inters inters ats ins ins:

df.loc[mask, 'header'] = df.loc[mask | ~m, 'header'].groupby(df['groups'])
                                                    .transform(lambda x: x.interpolate())

Idea is remove consecutive missing values without last, then use interpolate and assign back last missing value by condition:

m =  df['header'].isna()
mask = m & ~m.shift(-1, fill_value=False)

df.loc[mask, 'header'] = df.loc[mask | ~m, 'header'].interpolate()
print (df)
    header
0     10.0
1     20.0
2     20.0
3     20.0
4     30.0
5      NaN
6     35.0
7     40.0
8     10.0
9      NaN
10     NaN
11    30.0
12    50.0

Details:

print (df.assign(m=m, mask=mask))
    header      m   mask
0     10.0  False  False
1     20.0  False  False
2     20.0   True   True
3     20.0  False  False
4     30.0  False  False
5      NaN   True  False
6     35.0   True   True
7     40.0  False  False
8     10.0  False  False
9      NaN   True  False
10     NaN   True  False
11    30.0   True   True
12    50.0  False  False


print (df.loc[mask | ~m, 'header'])
0     10.0
1     20.0
2      NaN
3     20.0
4     30.0
6      NaN
7     40.0
8     10.0
11     NaN
12    50.0
Name: header, dtype: float64

Solution for interpolate per groups is:

df.loc[mask, 'header'] = df.loc[mask | ~m, 'header'].groupby(df['groups'])
                                                    .transform(lambda x: x.interpolate())
╰沐子 2025-02-10 17:31:37

您可以尝试:

s = df['header']
m = s.isna()
df['header'] = s.ffill().add(s.bfill()).div(2).mask(m&m.shift(-1, fill_value=False))

输出和中间体:

    header  output  ffill  bfill      m  m&m.shift(-1)
0     10.0    10.0   10.0   10.0  False          False
1     20.0    20.0   20.0   20.0  False          False
2      NaN    20.0   20.0   20.0   True          False
3     20.0    20.0   20.0   20.0  False          False
4     30.0    30.0   30.0   30.0  False          False
5      NaN     NaN   30.0   40.0   True           True
6      NaN    35.0   30.0   40.0   True          False
7     40.0    40.0   40.0   40.0  False          False
8     10.0    10.0   10.0   10.0  False          False
9      NaN     NaN   10.0   50.0   True           True
10     NaN     NaN   10.0   50.0   True           True
11     NaN    30.0   10.0   50.0   True          False
12    50.0    50.0   50.0   50.0  False          False

You can try:

s = df['header']
m = s.isna()
df['header'] = s.ffill().add(s.bfill()).div(2).mask(m&m.shift(-1, fill_value=False))

output and intermediates:

    header  output  ffill  bfill      m  m&m.shift(-1)
0     10.0    10.0   10.0   10.0  False          False
1     20.0    20.0   20.0   20.0  False          False
2      NaN    20.0   20.0   20.0   True          False
3     20.0    20.0   20.0   20.0  False          False
4     30.0    30.0   30.0   30.0  False          False
5      NaN     NaN   30.0   40.0   True           True
6      NaN    35.0   30.0   40.0   True          False
7     40.0    40.0   40.0   40.0  False          False
8     10.0    10.0   10.0   10.0  False          False
9      NaN     NaN   10.0   50.0   True           True
10     NaN     NaN   10.0   50.0   True           True
11     NaN    30.0   10.0   50.0   True          False
12    50.0    50.0   50.0   50.0  False          False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文