I have a data frame with three columns like this:
column1 column2 column3
1 2 3
2 1 3
3 1 2
2 3 1
I want to count how many 1s are there in each column. The expected output is like this:
column1: 1
column2: 2
column3: 1
my_dict = {}
for c in df.columns:
v = df[df[c]==1][c].sum()
my_dict[c]=v
result: my_dict
{'column1': 1, 'column2': 2, 'column3': 1}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments