DataFrame merges other columns based on whether multiple columns are the same

DataFrame merges other columns based on whether the values ​​in multiple columns are the same

For me, who has not learned much about this aspect, I really spent a lot of time searching for various information to implement this function (maybe I didn’t find it in the right way. If you have any recommended learning methods or resources, Especially related to csv file data analysis, please leave a message to let me know, thank you~)

Let’s get to the point with a vivid example.

First there is such a simple DataFrame, the code and effect are as follows:

import pandas as pd

df = pd.DataFrame(data=[['苹果', 5, 3],
                        ['苹果', 5, 6],
                        ['苹果', 7, 7],
                        ['桃子', 2, 6],
                        ['桃子', 2, 3]],
                  columns=['水果', '大小', '重量'],
                  index=None)
print(df)

Insert image description here

So the question is, how to add up the total weight of fruits of the same category and size ? (Perhaps you may wonder whether it is necessary to write a program for statistics. Can't you see it at a glance? Yes, because this is relatively simple. What if there are thousands of pieces of data)

It's actually very simple. The core is only one line of code, look here!

for (key1, key2), group in df.groupby(['水果', '大小']):
    print(group)
    print(key1, key2, group['重量'].sum())
    print('__________________')

Insert image description here

over,over

Guess you like

Origin blog.csdn.net/weixin_45606831/article/details/127198347