Plot from csv with panda grouping

Viewed 66

If I have a csv with 4 columns: how can I average the values of one column (x) over the average of another column (y) by grouping through the first one with panda? I have to do a loop for every value of the first column? I am not sure about the implementation.

For example, if I have a csv file:

a,1,2,4
a,2,2,5
a,3,2,6
a,4,2,5
b,1,3,2
b,2,3,3
b,3,3,4

and I want a plot with a,average(3rd column) and b,average(3rd column)

I have to do something like:

df=pd.reas_csv
x=group_by("values of the 1st column").average()

I would also try to plot kde over the 2nd column, which has ten rows for every group of the first column. I don't understand how to group data from *csv file without a header in particular.

Thank you for the help.

1 Answers

Assume your dataframe looks like

print(df)

   0  1  2  3
0  a  1  2  4
1  a  2  2  5
2  a  3  2  6
3  a  4  2  5
4  b  1  3  2
5  b  2  3  3
6  b  3  3  4

If you want to plot with a average of 3rd column and b average of 3rd column, you can do

import pandas as pd
import matplotlib.pyplot as plt

df.groupby(0).mean()[3].plot.bar(rot=0)

plt.show()

enter image description here

Related