Python 如何计算列中每种类型的标签并将其保存在变量中?

Python 如何计算列中每种类型的标签并将其保存在变量中?,python,pandas,dataframe,data-analysis,Python,Pandas,Dataframe,Data Analysis,我必须计算数据帧列中出现1和0的时间数。我尝试了以下代码: count_own_party = len(df['Deviation from Partisanship']== 1) count_opposing_party = len(df['Deviation from Partisanship'] == 0) print(count_own_party) print(count_opposing_party) 这两个值的输出相同:7854。虽然从中可以清楚地看到1的数量大于0的数量。为此

我必须计算数据帧列中出现1和0的时间数。我尝试了以下代码:

count_own_party = len(df['Deviation from Partisanship']== 1)
count_opposing_party = len(df['Deviation from Partisanship'] == 0)

print(count_own_party)
print(count_opposing_party)

这两个值的输出相同:7854。虽然从中可以清楚地看到1的数量大于0的数量。

为此,熊猫中有一个功能: :


以下是一个例子:

print(df)
    Mnth  Income
0    Jan      80
1    Feb      80
2    Mar      50
3  April      60
4    May      60



df['deference from partisship']==1返回相同长度的布尔数组
len(df[df[‘与党派的背离’]==1])
df[‘与党派的背离’]==1)
返回一个由1和0组成的布尔数组,其长度与序列本身相同。尝试
df['developeration from partisship']==1.sum()
代替。对于这一点,pandas中有一个函数,这是
系列。valu_counts
数据框的外观如何,您是否将每个人的偏差计数为1或0,或者它是一个总数,如果可以的话,一个小样本的数据会有所帮助
print(df)
    Mnth  Income
0    Jan      80
1    Feb      80
2    Mar      50
3  April      60
4    May      60
value_count=df['Income'].value_counts()
print(value_count)



60    2
80    2
50    1
Name: Income, dtype: int64
count_60 = value_count[60]
print(count_60)
#2