[英]Pandas apply groupby list to column containing list
I have the following dataframe df
:我有以下数据框df
:
Col1 Col2
A 'AD'
A ['FG', 'LKL']
B ['FGT']
I am trying the following:我正在尝试以下操作:
df.groupby('Col1')['Col2'].apply(list)
I get the following:我得到以下信息:
Col1 Col2
A ['AD', "['FG', 'LKL']"]
B ['FGT']
I need to get the following:我需要得到以下信息:
Col1 Col2
A ['AD', 'FG', 'LKL']
B ['FGT']
You can do explode
你可以做explode
out = df.explode('Col2').groupby('Col1')['Col2'].apply(list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.