创建一个采用未定义数量参数的定义

Creating a definition that takes undefined number of parameters

将如下代码转换为能够接受我们想要的尽可能多的数据帧的最佳方法是什么?

def q_grab(df, df2, df3, q): #accepts three dataframes and a column name. Looks up column in all dataframes and combine to one
    data = df[q], df2[q], df3[q]
    headers = [q+"_1", q+"_2", q+"_3"]
    data2 = pd.concat(data, axis = 1, keys=headers)
    return data2

q = 'covid_condition'
data2 = q_grab(df, df2, df3, q) #If I run function pid_set first, it will create new df based on pID it looks like

一种方法是使用 * 运算符来获取参数列表 (但命名你的最​​后一个论点,所以它不是列表的一部分):

像这样:

def q_grab(*dfs, q=None): # q is a named argument to signal end of positional arguments
    data = [df[q] for df in dfs]
    headers = [q+"_"+str(i) for i in range(len(dfs))]
    data2 = pd.concat(data, axis = 1, keys=headers)
    return data2

q = 'covid_condition'
data2 = q_grab(df, df2, df3, q=q)    

一个可能更简洁的替代方案是继续传递数据帧列表作为第一个参数:

 def q_grab(dfs,q):
   

调用:

 data2 = q.grab([df,df2,df3], q)

使用上面的函数代码