在groupby对象上运行value_counts之后下降频率列

时间:2019-02-24 08:03:54

标签: python pandas dataframe

这是我的输出结果。有没有一种方法可以消除所有行的频率?

输入:

by_city = city_df.groupby("city")
total_driver_city = by_city["driver_count"].value_counts()
total_driver_city

输出:

city                  driver_count
Amandaburgh           12              1.0
Barajasview           26              1.0
Barronchester         11              1.0
Bethanyland           22              1.0
Bradshawfurt          7               1.0
Brandonfort           10              1.0
Carriemouth           52              1.0
Christopherfurt       41              1.0
Colemanland           23              1.0
Davidfurt             23              1.0
Deanville             49              1.0
East Aaronbury        7               1.0
East Danielview       22              1.0
East Kaylahaven       65              1.0

2 个答案:

答案 0 :(得分:1)

或使用iloc

df = total_driver_city.reset_index().iloc[:,:-1]

答案 1 :(得分:0)

我相信您需要转换MultiIndex Series(但是如果所有值都是1,则会得到与city_df相同的列):

df = total_driver_city.reset_index(name='tmp').drop('tmp', axis=1)
print (df)
               city  driver_count
0       Amandaburgh            12
1       Barajasview            26
2     Barronchester            11
3       Bethanyland            22
4      Bradshawfurt             7
5       Brandonfort            10
6       Carriemouth            52
7   Christopherfurt            41
8       Colemanland            23
9         Davidfurt            23
10        Deanville            49
11   East Aaronbury             7
12  East Danielview            22
13  East Kaylahaven            65

df = total_driver_city.index.to_frame().reset_index(drop=True)
print (df)
               city  driver_count
0       Amandaburgh            12
1       Barajasview            26
2     Barronchester            11
3       Bethanyland            22
4      Bradshawfurt             7
5       Brandonfort            10
6       Carriemouth            52
7   Christopherfurt            41
8       Colemanland            23
9         Davidfurt            23
10        Deanville            49
11   East Aaronbury             7
12  East Danielview            22
13  East Kaylahaven            65