python-串联大熊猫groupby中的字符串

时间:2020-04-21 23:44:17

标签: python pandas group-by concatenation

我有这个数据框:

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'}})

enter image description here

我创建了这个函数:

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 功能内将其串联起来?

1 个答案:

答案 0 :(得分:2)

您可以将“位置”放置在列表中,并使用“ +”组合列表。

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