我有这个数据框:
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 功能内将其串联起来?
答案 0 :(得分:2)
您可以将“位置”放置在列表中,并使用“ +”组合列表。
df_.groupby(col_names+["Location"])