How can I count the number of occurrences of an element across two columns of a dataframe?

smhirl2000

I have a dataframe with columns: "Color", "Size", "Shape", and "Being Sold" to describe various fruits. The "Being Sold" column contains boolean elements that describe whether an Item is being sold.

I ultimately want to create a plot that shows the number of fruits that are being sold for each color. I can get the list of all unique colors as well as the number of fruits that fall into each color category with these two lines respectively:

print(pd.unique(df["Color"]))
print(df["Color"].value_counts())

However, I need to add the qualifier "Being Sold = true" so that the second line does not include fruits that aren't being sold. What's the easiest way to go about doing this?

Sheldore

You can access the dataframe corresponding to only "Being Sold" = True using the following

df[df["Being Sold"]] # or df[df["Being Sold"]=="true"] # if strings 

Since "Being Sold" is Boolean column, you don't need to write df[df["Being Sold"]==True]. Although it will work too.

Then, you can simply do whatever operation you want. For ex.

df[df["Being Sold"]]["Color"].value_counts()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count occurrences in DataFrame across columns

R: How to count the number of occurrences of a value across multiple columns?

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

how can I split a dataframe by two columns and count number of rows based on group more efficient

How to count occurrences in a dataframe columns?

How can I count the occurrences of two date columns and show it in only one table?

How can I count the total number of occurrences at time step t of an element?

Count occurrences of factors across multiple columns in grouped dataframe

How to count DISTINCT occurrences of a single value across two different columns in PostgreSQL?

How can I count the number of occurrences in a string based on character letter?

How can I count the number of occurrences of a simple pattern in a string?

How can I count occurrences of a string in a dataframe in Python?

Count the number of occurrences each combination of values in two columns of a pandas dataframe and make a barplot

How can I count replicate values across columns?

How can I perform aggregation across two separate columns in SQL?

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 the number of occurrences based on uniques values in another columns dataframe in pandas? Please Consider the below example

How to auto number across two columns in a table

Merge two columns in a single DataFrame and count the occurrences using PySpark

How to 'count' number of non-empty values in a single row across multiple columns in a dataframe

PostgreSQL: Count Number of Occurrences in Columns

Count occurrences across different columns with the same information

How can I use JavaScript to count the number of occurrences of a div class, and print the number to screen without jQuery?

how to compare two columns in two different data frames and count the occurrences

How can I Count the number of columns in a .txt file in VBA

How can I count the number of columns a value has after a Where?

Assign Group Number to Dataframe - Matching across two columns

How can I count occurrences with groupBy?

How can I count occurrences of a value?