[英]Pandas groupby make two columns lists separately
I currently have a DataFrame that has three columns. 我目前有一个包含三列的DataFrame。 Let's call them fruit
, sport
, and weather
for the sake of convenience. 为了方便起见,我们称它们为fruit
, sport
和weather
。
What I want to do is to group the DataFrame by the fruit
column and make the corresponding values for sport
and weather
into lists so that we'll have corresponding sport
and weather
lists for each unique fruit
. 我想要做的是将DataFrame按fruit
列分组,并将相应的sport
和weather
值归入列表,以便为每个独特的fruit
提供相应的sport
和weather
列表。
For example: 例如:
# Original DataFrame
fruit sport weather
0 apple baseball sunny
1 banana swimming cloudy
2 apple basketball windy
3 orange football sunny
4 banana hockey windy
# Desired DataFrame
fruit sport weather
0 apple [baseball, basketball] [sunny, windy]
1 banana [swimming, hockey] [cloudy, windy]
2 orange [football] [sunny]
Grouping one of the column values into a list is relatively straightforward, but I'm a bit stuck as to how to do that with two. 将一个列值分组到一个列表中是相对简单的,但是我对如何使用两个列值有点困惑。 How might I go about that? 我该怎么办? Thanks in advance. 提前致谢。
You can groupby
and aggregate witht the list
constructor: 您可以对list
构造器进行groupby
和聚合:
df.groupby('fruit', as_index=False).agg(list)
fruit sport weather
0 apple [baseball, basketball] [sunny, windy]
1 banana [swimming, hockey] [cloudy, windy]
2 orange [football] [sunny]
您可以使用pivot_table
表方法:
df.pivot_table(index='fruit', aggfunc=list).reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.