python – Pandas Group by 2列和T和F的计数实例创建2个新列

参见英文答案 >
How to pivot a dataframe                                    1个

我有一个包含3列的pandas数据框:source_name,dest_address和fall_between.我想按前两列进行分组,并根据fall_between列创建2个新列.这就是df的样子:

df           
   source_name  dest_address   fall_between
0  source_1     72.21.215.90   False
1  source_1     72.21.215.90   False
2  source_1     72.21.215.90   False
3  source_1     72.21.215.90   False
4  source_1     131.107.0.89   False
5  source_1     131.107.0.89   False
6  source_2     69.63.191.1    False
7  source_2     69.63.191.1    True
8  source_2     69.63.191.1    True
9  source_2     69.63.191.1    True
10 source_2     69.63.191.1    True

期望的输出:

df
   source_name  dest_address   true_count  false_count
0  source_1     72.21.215.90   0           4
1  source_1     131.107.0.89   0           2  
2  source_2     69.63.191.1    4           1

我正在使用以下内容,但如果它是0,我没有得到计数.有什么更好的方法呢?

df[df['fall_between'] == True].groupby(['source_name','dest_address']).size().reset_index(name='true_count')
df[df['fall_between'] == False].groupby(['source_name','dest_address']).size().reset_index(name='false_count')

最佳答案 你可以使用
pd.crosstab

pd.crosstab([df.source_name, df.dest_address], df.fall_between).reset_index()

fall_between  source_name  dest_address  False  True
0               source_1  131.107.0.89      2     0
1               source_1  72.21.215.90      4     0
2               source_2   69.63.191.1      1     4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞