在特定列中连续计算特定整数值

时间:2019-08-20 18:48:02

标签: python pandas dataframe count

我有pandas数据框,其中有12列,每列的值随机为0到12。我想创建新的列,其中将包含这些特定值的计数。例如newvar1将在每行中包含计数“ 1”。另一个新的结果变量将在某些特定列中包含某个特定值的计数

我的数据框是这样的

v1 v2 v3 v4 
0   1  2  1 
2   3  1  1 
我想要的

输出就像

v1 v2 v3 v4 newvar1_count_of_1 newvar1_count_of_1_ in_first_2_col
0   1  2  1       2                            1
2   3  1  1       2                            0

1 个答案:

答案 0 :(得分:0)

您只应该这样做:

df1['newvar1_count']=df1[df1.eq(1)].sum(axis=1)
df1['newvar1_count_of_1_ in_first_2_col']=df1[df1.eq(1)].loc[:,['v1','v2']].sum(axis=1)

适用于您的代码:

import pandas as pd
import numpy as np
df1=pd.DataFrame()
df1['v1']=[0,2]
df1['v2']=[1,3]
df1['v3']=[2,1]
df1['v4']=[1,1]
df1['newvar1_count']=0
df1['newvar1_count_of_1_ in_first_2_col']=0
df1['newvar1_count']=df1[df1.eq(1)].sum(axis=1)
df1['newvar1_count_of_1_ in_first_2_col']=df1[df1.eq(1)].loc[:,['v1','v2']].sum(axis=1)
df1

输出:

    v1  v2  v3  v4  newvar1_count   newvar1_count_of_1_ in_first_2_col
  0 0   1   2   1   2.0             1.0
  1 2   3   1   1   2.0             0.0