熊猫:填充列中具有同一组的值

发布于 2025-02-10 06:12:48 字数 357 浏览 3 评论 0 原文

我需要填充列中的无空值,而不是同一组的零值。

示例

所需的结果

我尝试使用变换与模式使用,但它没有完成工作。

test['col2']=test['col2'].transform(lambda x:x.fillna(x.mode())

I need to fill null values in the column with not null value of the same group.

Example

Desired Outcome

I tried using transform with mode, but it didn't do the job.

test['col2']=test['col2'].transform(lambda x:x.fillna(x.mode())

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

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

发布评论

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

评论(2

无语# 2025-02-17 06:12:48

使用 使用模式,如果存在,则选择第一个值,else none ,最后传递给

s = df.groupby('col1')['col2'].transform(lambda x: next(iter(x.mode()), None))
df['col2'] = df['col2'].fillna(s)
print (df)
  col1   col2
0  gr1  test1
1  gr2  test2
2  gr1  test1
3  gr1  test1
4  gr2  test2
5  gr3  test3
6  gr2  test2

Use GroupBy.transform with mode and select first value if exist, else None, last pass to Series.fillna:

s = df.groupby('col1')['col2'].transform(lambda x: next(iter(x.mode()), None))
df['col2'] = df['col2'].fillna(s)
print (df)
  col1   col2
0  gr1  test1
1  gr2  test2
2  gr1  test1
3  gr1  test1
4  gr2  test2
5  gr3  test3
6  gr2  test2
痴骨ら 2025-02-17 06:12:48

我将使用 .assign .apply 遍历每一行,然后找到模式:

import pandas
import numpy

df = pandas.DataFrame({
    'col1':['gr1', 'gr2', 'gr1', 'gr1', 'gr2', 'gr3', 'gr2', numpy.nan], 
    'col2':['test1', 'test2', 'test', numpy.nan, numpy.nan, 'test3', numpy.nan, numpy.nan],
})

def fill_value(x):
    if x['col2'] is numpy.nan:
        mode = df.loc[df['col1'] == x['col1'], 'col2'].mode()
        default = numpy.nan
        return mode.iloc[0] if not mode.empty else default
    else:
        return x['col2']
    
df = df.assign(col2=df.apply(fill_value, axis=1))

输出:

  col1   col2
0  gr1  test1
1  gr2  test2
2  gr1   test
3  gr1   test
4  gr2  test2
5  gr3  test3
6  gr2  test2
7  NaN    NaN

I would use .assign and .apply to go through each row and then find the mode:

import pandas
import numpy

df = pandas.DataFrame({
    'col1':['gr1', 'gr2', 'gr1', 'gr1', 'gr2', 'gr3', 'gr2', numpy.nan], 
    'col2':['test1', 'test2', 'test', numpy.nan, numpy.nan, 'test3', numpy.nan, numpy.nan],
})

def fill_value(x):
    if x['col2'] is numpy.nan:
        mode = df.loc[df['col1'] == x['col1'], 'col2'].mode()
        default = numpy.nan
        return mode.iloc[0] if not mode.empty else default
    else:
        return x['col2']
    
df = df.assign(col2=df.apply(fill_value, axis=1))

output:

  col1   col2
0  gr1  test1
1  gr2  test2
2  gr1   test
3  gr1   test
4  gr2  test2
5  gr3  test3
6  gr2  test2
7  NaN    NaN
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文