【发布时间】: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