Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
import pandas as pd
import plotly.express as px
url='https://gist.githubusercontent.com/shinokada/f2ebf82eaa3cfa02106ae93c7fe9efbe/raw/3494b1e2ba9b1c0d8991e71483123f63b4bce559/educational+attainment.csv'
df = pd.read_csv(url)
display(df)
cols = [2,10,14]
rows = [33,36,39,42,45,48,51,57]
df =df.iloc[rows,cols].reset_index(drop=True)
df.columns=['race','Male','Female']
for col in ['Male', 'Female']:
df[col]=pd.to_numeric(df[col].str.replace('%',''))
display(df)
print(df.dtypes)
race object
Male float64
Female float64
dtype: object
df=pd.melt(df,id_vars=['race'],var_name='gender', value_name='value')
df
fig = px.bar(df, x="race", color="gender",
y='value',
title="A Grouped Bar Chart With Plotly Express in Python",
barmode='group',
height=600
)
fig.show()
fig = px.bar(df, x="race", color="gender",
y='value',
title="A Grouped Bar Chart With Plotly Express in Python",
barmode='group',
height=700,
facet_row="gender"
)
fig.show()