How can I count unique values by columns

alisonggg

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
adhg
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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I count unique values in Excel?

How can I count the unique values from all columns and display them in a separate dataframe w.r.t. their unique name?

How can I count values that can be grouped by another columns in R

How can I count the unique values in a Pandas Dataframe?

How to count number of unique values in N columns

How can I count replicate values across columns?

How can I count the amount of values in different columns in oracle plsql

How Do I Count Unique Values in Excel

How can I count the number of occurrences of a range of values in one column, against all unique values in another, in MySQL?

How can I count occurrences of unique items per column across columns in pandas?

How can I count unique fields in a file?

How compare and count the number of unique values across multiple columns

After comparing two columns, how can I remove unique values in one column with its values in other different columns in R

How can I group by and summarize while keeping unique values by group and count their occurence?

SQL: How can I count unique attribute values using two attributes and joining two tables?

How can I count unique attribute values using two attributes and joining two tables?

How can I count the min/max item for for a set of otherwise unique values within a dataframe in R?

How, in python, can I count unique values in a column for gradually increasing numbers of rows within groups

How can I groupby a DataFrame at the same time I count the values and put in different columns?

How can I select unique values from several columns in Oracle SQL?

How can I get the unique values of a column and turn them into new columns using SELECT?

How can I count columns in phalcon volt?

how I can count signs columns?

How can I group with multiple columns and count?

How to put unique values of 1 series as columns and count each occurence of the unique values from the series per quarter?

How can I filter unique values in react?

How do I get unique id count across columns in R?

How can I write an excel formula to count the number of times values in two separate columns are the same on the same row

How to count columns with values