使用pd.pivot_table Without Values参数的熊猫行计数

时间:2019-07-17 15:31:52

标签: pandas pivot-table

如何在不将任何内容传递给values参数的情况下使用Pandas数据透视表获取行数?如果执行以下操作,则生成的数据透视表将具有“年龄”和“名称”列的行数,这听起来并不干净。

import pandas as pd
import numpy as np

visitor = [['Jack', 34, 'Australia', 'Sydney'] ,
             ['Riti', 30, 'India', 'Delhi' ] ,
             ['Aadi', 16, 'United States', 'New York'], 
             ['Mary', 22, 'United States', 'New York'], 
             ['Doug', 13, 'United States', 'Los Angeles'],
             ['Chad', 15, 'Australia', 'Sydney'],
             ['Alba', 32, 'United Kingdom', 'London']]

df = pd.DataFrame(visitor, columns=['Name','Age','Country', 'City'])

pd.pivot_table(df, index=['Country', 'City'], aggfunc='count')

输出数据框

                                Age    Name
Country           City           
Australia         Sydney         2      2
India             Delhi          1      1
United Kingdom    London         1      1
United States     Los Angeles    1      1
                  New York       2      2

在SQL中,我可以

SELECT country, city, count(*) FROM visitor GROUP BY country, city

并且没有不必要的列添加到输出表,感觉更干净。

                                count   
Country           City           
Australia         Sydney         2  
India             Delhi          1 
United Kingdom    London         1 
United States     Los Angeles    1 
United States     New York       2

1 个答案:

答案 0 :(得分:1)

只需pd.groupby

res = df.groupby(['Country','City'],as_index=False)['Name'].count().rename(columns={"Name":"Count"})
print(res)
    Country         City    Count
0   Australia       Sydney      2
1   India           Delhi       1
2   United Kingdom  London      1
3   United States   Los Angeles 1
4   United States   New York    2