Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

python - Apply get_group() when groupby used multiple columns

I applied Pandas groupby to a dataframe to get all available combinations of a few fields as follows:

list_of_fields = [field1, field2, field3, field4]
grouped = df.groupby(list_of_fields)

This works as expected and when I print the groups in grouped.groups I get the right tuple combinations.

Now I need to use the groups in grouped to create multiple dataframes with the rows that match the values for each of the groups (basically, splitting my original dataframe). However, I do not know how to do this. I have tried the following:

for key in grouped.groups.keys():
    partial_df = grouped.get_group(key)

But this is giving me a key error. Is this because the keys are multiple fields? How could I make it work? Thanks in advance!

question from:https://stackoverflow.com/questions/65867283/apply-get-group-when-groupby-used-multiple-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can cast the groupby object into a list:

list_of_fields = ['a','b','c']
df = pd.DataFrame({'a':list('110100111'),
                   'b':list('233322233'),
                   'c':list('555444454'),
                   'd':list('xyxyxyxyy')})

grouped = df.groupby(list_of_fields)
dfs = [x[1] for x in list(grouped)]

print(dfs[0])
#   a  b  c  d
#4  0  2  4  x
#5  0  2  4  y

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...