簡體   English   中英

Pandas pivot_table 按值對列進行分組

[英]Pandas pivot_table group column by values

我正在嘗試使用數值作為 Pandas 數據透視表上的列。 問題在於,由於每個數字大多是唯一的,因此生成的 pivot_table 作為聚合數據的方法並不是很有用。

這是我到目前為止所擁有的(假數據示例):

import pandas as pd   

df = pd.DataFrame({'Country': ['US', 'Brazil', 'France', 'Germany'], 
                       'Continent': ['Americas', 'Americas', 'Europe', 'Europe'], 
                       'Population': [321, 207, 80, 66]})


pd.pivot_table(df, index='Continent', columns='Population', aggfunc='count')

這是結果的圖像數據透視表 .

如何根據我的列將我的值分組到范圍內?

換句話說,我如何計算所有人口... <100、100-200、> 300的國家?

使用 pd.cut:

df = df.assign(PopGroup=pd.cut(df.Population,bins=[0,100,200,300,np.inf],labels=['<100','100-200','200-300','>300']))

輸出:

  Continent  Country  Population PopGroup
0  Americas       US         321     >300
1  Americas   Brazil         207  200-300
2    Europe   France          80     <100
3    Europe  Germany          66     <100

pd.pivot_table(df, index='Continent', columns='PopGroup',values=['Country'], aggfunc='count')

輸出:

        Country          
PopGroup  200-300 <100 >300
Continent                  
Americas      1.0  NaN  1.0
Europe        NaN  2.0  NaN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM