Advanced pandas processing-grouping and aggregation

Advanced pandas processing-grouping and aggregation


Grouping and aggregation is usually a way to analyze data. It is usually used together with some statistical functions to view the grouping of data  [In pandas, it must be connected together and cannot be used alone. It is meaningless to talk about grouping without aggregation]

Think about the fact that the cross-tab and pivot table just now also have the function of grouping, so they are a form of grouping, but they mainly count the number of times or calculate the ratio! ! See the effect:

 

1 What grouping and aggregation

 

2 Group API

  • DataFrame.groupby(key, as_index=False)  [as_index: whether to index] [data can be grouped multiple times, you need to pass a list inside to complete]
    • key: grouped column data, can be multiple
  • Case: Price data of different pens in different colors
col =pd.DataFrame({'color': ['white','red','green','red','green'], 'object': ['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.30,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]})

color    object    price1    price2
0    white    pen    5.56    4.75
1    red    pencil    4.20    4.12
2    green    pencil    1.30    1.60
3    red    ashtray    0.56    0.75
4    green    pen    2.75    3.15
  • Group, group colors, aggregate price
# 分组,求平均值
col.groupby(['color'])['price1'].mean()
col['price1'].groupby(col['color']).mean()  #  这两种方式是一样的

color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64

# 分组,数据的结构不变
col.groupby(['color'], as_index=False)['price1'].mean()

color    price1
0    green    2.025
1    red    2.380
2    white    5.560

3 Starbucks retail store data

Now we have a set of statistics about Starbucks stores around the world. What if I want to know the number of Starbucks in the US and China, or I want to know the number of Starbucks in each province in China, then what should I do?

Data source: https://www.kaggle.com/starbucks/store-locations/data

 

3.1 Data acquisition

Read Starbucks store data from the file

# 导入星巴克店的数据
starbucks = pd.read_csv("./data/starbucks/directory.csv")

3.2 Perform group aggregation

# 按照国家分组,求出每个国家的星巴克零售店数量
count = starbucks.groupby(['Country']).count()  #  因为此行输出一样,所以取任意一行进行统计(Brand)

Draw a picture to show the result

count['Brand'].plot(kind='bar', figsize=(20, 8))
plt.show()

 

Suppose we join the provinces and cities to group together

# 设置多个索引,set_index()
starbucks.groupby(['Country', 'State/Province']).count()

Take a closer look at this structure. Which structure is similar to the one mentioned earlier? ?

Similar to the previous MultiIndex structure

4 summary

  • groupby for data grouping
    • In pandas, it is meaningless to talk about grouping without aggregation

Guess you like

Origin blog.csdn.net/weixin_44799217/article/details/114197870