【问题标题】:Pandas aggregation warning with lambdas (FutureWarning: using a dict with renaming is deprecated)带有 lambdas 的 Pandas 聚合警告(FutureWarning:不推荐使用带有重命名的 dict)
【发布时间】:2018-03-28 04:18:01
【问题描述】:

我的问题类似于this one,但是我确实需要重命名列,因为我使用函数聚合我的数据:

def series(x):
    return ','.join(str(item) for item in x)

agg = {
    'revenue': ['sum', series],
    'roi': ['sum', series],
}

df.groupby('name').agg(agg)

因此,我有一组同名的列:

在我删除更高的列级别后变得完全无法区分:

df.columns = df.columns.droplevel(0)

那么,我该如何为我的列保留唯一的名称?

【问题讨论】:

标签: python pandas pandas-groupby


【解决方案1】:

map 用于flatten 列名称:

df.columns = df.columns.map('_'.join)

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多