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?
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.
Comments