I have the following dataframe
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
# 3.5.3
df=pd.DataFrame({'Type': [ 'Sentence', 'Array', 'String', '-','-', 'Sentence', 'Array', 'String', '-','-', 'Sentence'],
'Length': [42,21,11,6,6,42,21,11,6,6,42],
'label': [1,1,0,0,0,1,1,0,0,0,1],
})
print(df)
# Type Length label
#0 Sentence 42 1
#1 Array 21 1
#2 String 11 0
#3 - 6 0
#4 - 6 0
#5 Sentence 42 1
#6 Array 21 1
#7 String 11 0
#8 - 6 0
#9 - 6 0
#10 Sentence 42 1
I want to plot stacked bar chart for the arbitrary column within dataframe (either numerical e.g. Length
column or
CodePudding user response:
The values in Expected output do not match
df
in the OP, so the sample DataFrame has been updated.-
DataFrame Views
df
Type Length label 0 Sentence 42 1 1 Array 21 1 2 String 11 0 3 - 6 0 4 - 6 0 5 Sentence 42 1 6 Array 21 1 7 String 11 0 8 - 6 0 9 - 6 1 10 Sentence 42 0
dfp
label 0 1 Type - 3.0 1.0 Array NaN 2.0 Sentence 1.0 2.0 String 2.0 NaN
total
Type - 4.0 Array 2.0 Sentence 3.0 String 2.0 dtype: float64
per
label 0 1 Type - 75.00 25.00 Array NaN 100.00 Sentence 33.33 66.67 String 100.00 NaN