【问题标题】:python - Concatenate strings inside pandas groupbypython - 在pandas groupby中连接字符串
【发布时间】:2020-04-21 23:44:17
【问题描述】:

我有这个数据框:

df = pd.DataFrame({'A': {0: '1',  1: '2',  2: '4',  3: '7',  4: '7'},
 'B': {0: 'S', 1: 'S', 2: 'D', 3: 'D', 4: 'S'},
 'C': {0: 'XX',  1: 'WX',  2: 'WX',  3: 'XX',  4: 'XW'},
 'Location': {0: '32',  1: '63',  2: '32',  3: '42',  4: '42'}})

我创建了这个函数:

def Transformation(df_, col_names):
    # function code (irrelevant for the problem statement)
    df_.groupby([col_names,"Location"]) # the line problem
    # function code (irrelevant for the problem statement)
    return df_ # (irrelevant for the problem statement)

Transformation(z, ["A", "B"]) # How you call the function. col_names has to be more than 1.

# the line problem 上面:如何在 groupby 参数中连接col_names"Location" ?你可以假设dimensions 总是作为一个包含多个元素的字符串列表给出,就像这样:

Transformation(df, ["A", "B"])
Transformation(df, ["C", "A"])
Transformation(df, ["A", "B", "C", "D"]) # You can assume that the whole abecedary is in the columns of `df` and you can combine them as you wish, but for minimal example purposes I think two is enough

"Location" 不能进入 dimensions 参数(出于函数目的),如果这样做,函数将引发错误。因此,假设"Location" 从未出现在输入参数中,而是添加在函数代码中的某处,而当我添加"Location" 时,我遇到了问题。

我使用的一种方法,我不明白为什么不起作用:

df_.groupby(col_names.append("Location"))

这导致我:

x = ["A","B", "C"]
x_aux = x.append("Location")
x_aux # gives "None"

但是!:

x = ["A","B", "C"]
x.append("Location")
x # gives ["A","B", "C", "Location"]

为什么会这样? 在 groupby 函数中连接它有什么建议吗?

【问题讨论】:

    标签: python pandas group-by concatenation


    【解决方案1】:

    您可以将 Location 放在一个列表中并使用“+”来连接列表。

    df_.groupby(col_names+["Location"])
    

    【讨论】:

    • 嗯,这很简单。你知道我给x_aux = x.append("Location")的例子为什么给None吗?
    • @Chris,append 函数改变了列表 x 但返回 None。这就是为什么当您将 x.append(...) 分配给 x_aux 时,x_aux 为 None,因为新元素直接附加到 x。
    猜你喜欢
    • 1970-01-01
    • 2015-11-14
    • 2015-02-02
    相关资源
    最近更新 更多