Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

pandas - Obtain a barplot from boolean values?

I'm facing a bit of a problem. This is my dataframe:

Students     Subject       Mark
1            M F           7 4 3 7
2            I             5 6 
3            M F I S       2 3 0 
4            M             2 2 
5            F M I         5 1
6            I M F         6 2 3
7            I M           7

I want to plot a barplot with four "bars", for students respecting the next four conditions:

  • Have 3 ore more letters in the column "Subject"
  • Have at least one 3 in the colum "Marks"
  • Have both things
  • Have neither things

At first I was stuck, but I was suggested to proceed this way:

df["Subject"].str.count("w+") >= 3

df["Mark"].str.count("3") >= 1

(df["Subject"].str.count("w+") >= 3) & (df["Mark"].str.count("3") >= 1)

What I obtain are three boolean columns, but I don't know how to go from here to plot the barplot. I was thinking about counting the values in each column, but I don't seem to find a way to do so, since it looks like I can't apply value_counts() to the boolean columns.

If you have any idea, please help!

question from:https://stackoverflow.com/questions/65843129/obtain-a-barplot-from-boolean-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need create DataFrame with all 4 masks, then count Trues by sum and last plot:

m1 = df["Subject"].str.count("w+") >= 3
m2 = df["Mark"].str.count("3") >= 1

df1 = pd.concat([m1, m2, m1 & m2, ~m1 & ~m2], axis=1, keys=('a','b','c','d'))

out = df1.sum()

If need seaborn solution:

import seaborn as sns

ax = sns.barplot(x="index", y="val", data=out.reset_index(name='val'))

For pandas (matplotlib) solution:

out.plot.bar()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...