我得到了以下氨纶:

   index  user  default_shipping_cost     category  shipping_cost  shipping_coalesce  estimated_shipping_cost
0      0     1                      1      clothes            NaN                1.0                      6.0
1      1     1                      1  electronics            2.0                2.0                      6.0
2      2     1                     15    furniture            NaN               15.0                      6.0
3      3     2                     15    furniture            NaN               15.0                     15.0
4      4     2                     15    furniture            NaN               15.0                     15.0

每个用户,将shipping_cost与默认_shipping_cost结合起来,并计算合并shipping_costs的平均值,但前提是至少有一个给出的shipping_costs.

解释:

  • user_1给出了shipping_cost(至少一次),以便我们可以计算平均值
  • user_2没有shipping_cost,所以我想和Nan一起go

代码 :

import pandas as pd

pd.set_option("display.max_columns", None)
pd.set_option("display.max_rows", None)
pd.set_option('display.width', 1000)

df = pd.DataFrame(
    {
        'user': [1, 1, 1, 2, 2],
        'default_shipping_cost': [1, 1, 15, 15, 15],
        'category': ['clothes', 'electronics', 'furniture', 'furniture', 'furniture'],
        'shipping_cost': [None, 2, None, None, None]
    }
)
df.reset_index(inplace=True)
df['shipping_coalesce'] = df.shipping_cost.combine_first(df.default_shipping_cost)

dfg_user = df.groupby(['user'])
df['estimated_shipping_cost'] = dfg_user['shipping_coalesce'].transform("mean")
print(df)

预期yields :

   index  user  default_shipping_cost     category  shipping_cost  shipping_coalesce  estimated_shipping_cost
0      0     1                      1      clothes            NaN                1.0                      6.0
1      1     1                      1  electronics            2.0                2.0                      6.0
2      2     1                     15    furniture            NaN               15.0                      6.0
3      3     2                     15    furniture            NaN               15.0                      NaN
4      4     2                     15    furniture            NaN               15.0                      NaN

推荐答案

添加transform('any')where的额外条件:

df['estimated_shipping_cost'] = (dfg_user['shipping_coalesce'].transform('mean')
                                .where(dfg_user['shipping_cost'].transform('any'))
                                )

输出:

   index  user  default_shipping_cost     category  shipping_cost  shipping_coalesce  estimated_shipping_cost
0      0     1                      1      clothes            NaN                1.0                      6.0
1      1     1                      1  electronics            2.0                2.0                      6.0
2      2     1                     15    furniture            NaN               15.0                      6.0
3      3     2                     15    furniture            NaN               15.0                      NaN
4      4     2                     15    furniture            NaN               15.0                      NaN

中级:

dfg_user['shipping_cost'].transform('any')

0     True
1     True
2     True
3    False
4    False
Name: shipping_cost, dtype: bool

Python相关问答推荐

如何使用上下文管理器创建类的实例?

Python plt.text中重叠,包adjust_text不起作用,如何修复?

分组数据并删除重复数据

Pydantic 2.7.0模型接受字符串日期时间或无

如何计算两极打印机中 * 所有列 * 的出现次数?

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

即使在可见的情况下也不相互作用

抓取rotowire MLB球员新闻并使用Python形成表格

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

数据抓取失败:寻求帮助

如何设置视频语言时上传到YouTube与Python API客户端

所有列的滚动标准差,忽略NaN

Odoo 16使用NTFS使字段只读

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

在嵌套span下的span中擦除信息

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

为用户输入的整数查找根/幂整数对的Python练习

没有内置pip模块的Python3.11--S在做什么?

Matplotlib中的曲线箭头样式