Home > Software engineering >  Python: histogram of each column per age
Python: histogram of each column per age

Time:01-28

I have built a table using the group by and sum function in python that looks like the following now:

age col_1 col_2 col_3
20 12 67 101
21 45 89 149
22 34 93 90

What I need to do is to built 3 plots that has the age in the x axis and has values in col_1, col_2 and col_3 in the y axis. I just need to see the differences in col_1, col_2 and col_3 per ages. I can't find something similar as a reference. If anyone can help, that'd be great! Thank you.

CodePudding user response:

You can try this:

df.plot(x="age", y=["col_1", "col_2", "col_3"], kind="bar",figsize=(9,8))
plt.show()

CodePudding user response:

You can use iloc to get the rows for instance:

x =  df['age']

y1 = df.iloc[0][1:] # first row without age
...

and then plot with matplotlib:

plt.plot(x, y1)

...

CodePudding user response:

Easiest to plot the df :

df = df.set_index('age')
df.plot(kind='bar')
plt.show()

output:

enter image description here

  •  Tags:  
  • Related