简体   繁体   English

如何在运行时在 Pandas DataFrame 上构建语句以执行 groupby 操作?

[英]How to build a statement to perform a groupby operation during runtime on a Pandas DataFrame?

I have a Pandas DataFrame dfs and a list headers我有一个 Pandas DataFrame dfs和一个列表headers

The list headers is assigned the column names of the DataFrame dfs during runtime.列表headers在运行时被分配了 DataFrame dfs的列名。

For ex, let us consider the list gets assigned with dfs 's column names as:例如,让我们考虑使用dfs的列名分配列表:

["Information_type", "Interface", "Type_of_Interface", "Connection_Mechanism"]

I want to perform the below groupby and agg operation on the DataFrame dfs without explicitly mentioning the column names in the groupby operation ie "Information_type": " ".join , "Interface": " ".join , "Type_of_Interface": " ".join , "Connection_Mechanism": " ".join :我想在 DataFrame dfs上执行以下groupbyagg操作,而无需明确提及 groupby 操作中的列名,即"Information_type": " ".join ”.join, "Interface": " ".join :“”.join, "Type_of_Interface": " ".join , "Connection_Mechanism": " ".join :

dfs[0]=dfs[0].groupby("grp").agg({"Information_type": " ".join, "Interface": " ".join, "Type_of_Interface": " ".join, "Connection_Mechanism": " ".join})

Basically write "Information_type": " ".join, "Interface": " ".join, "Type_of_Interface": " ".join, "Connection_Mechanism": " ".join to the above line in runtime.基本上在运行时将"Information_type": " ".join, "Interface": " ".join, "Type_of_Interface": " ".join, "Connection_Mechanism": " ".join写入上述行。

It would be great if such a thing would be possible, else I would have to manually edit the column names and execute the groupby and agg operation for each table!如果这样的事情是可能的,那就太好了,否则我将不得不手动编辑列名并为每个表执行groupbyagg操作!

Appreciate your help.感谢你的帮助。 Thanks in advance!提前致谢!

IIUC this is what you want: IIUC 这就是你想要的:

#setup
df = pd.DataFrame({'a':np.random.randint(0,5,25),
                   'b':np.random.randint(0,5,25),
                   'c':np.random.randint(0,5,25), 
                   'd':np.random.randint(0,5,25)}, dtype = str)

cols = ['b','c']

df.groupby('a').agg({col: " ".join for col in cols})

Output Output

               b              c
a                              
0  0 0 3 3 4 2 3  3 3 4 0 4 3 2
1      2 4 1 2 1      3 0 2 1 3
2        0 0 4 2        1 3 1 3
3    2 2 4 1 3 0    3 1 1 1 2 0
4          4 2 0          2 0 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM