Home > Software engineering >  How can I count how many rows have a value equal to 3 in each column and store them in a new column
How can I count how many rows have a value equal to 3 in each column and store them in a new column

Time:03-11

I am new to python. So I am trying to sum the total of each row/column with a value "3", and store it in a new column in my dataframe.

my data looks like this:

    [A] [B] [C] [D] [E] [F]

ID1  1   2   2   3   1   1
ID2  3   1   2   3   1   1
ID3  1   1   2   1   1   2
ID4  3   2   2   3   1   1
ID5  1   1   2   3   1   3

So I want to create a new column named "G" that count how many times each "ID" have a value of 3.

Can someone help, please.

CodePudding user response:

Use:

df['G'] = df.eq(3).sum(axis=1)
print(df)

# Output
     A  B  C  D  E  F  G
ID1  1  2  2  3  1  1  1
ID2  3  1  2  3  1  1  2
ID3  1  1  2  1  1  2  0
ID4  3  2  2  3  1  1  2
ID5  1  1  2  3  1  3  2

CodePudding user response:

This should give you what you need

data = {'A' : ['ID1', 'ID1', 'ID1', 'ID1', 'ID1'], 'A' : [1, 3, 1, 3, 1], 'B' : [2, 1, 1, 2, 1], 'C' : [2, 2, 2, 2, 2], 'D' : [3, 3, 1, 3, 3] } 
df = pd.DataFrame(data)
df['G'] = df[list(df.columns)].apply(lambda x : sum(1 for row_data in x if row_data == 3), axis = 1)
df
  • Related