Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Sort and plot values with 2 columns

Tags:

python

pandas

I met some difficulties because I'm a real beginners with Algo and code. I've got a set of data that I want to analyze.

My objective is to have an average value of the measurement depending of the name.

I have a dataframe with two columns:

Name Measurement value
PN1 5.4
PN2 6.5
PN3 9.6
PN2 5.4
PN4 7.4
PN2 5.4
... ...
... ...

What I want to obtain is something like that: Name with average value measurement by name :

Name AVG Measurement value
PN1 5.4
PN2 6.5
PN3 9.6
PN4 5.4

I'm a beginners in Pandas so I need help.

I've no idea how to proceed

like image 392
JibMich Avatar asked Mar 04 '26 13:03

JibMich


1 Answers

You can use groupby():

import pandas as pd

D = {
    'Name': ['PN1', 'PN2', 'PN3', 'PN2', 'PN4', 'PN2'],
    'Measurement value': [5.4, 6.5, 9.6, 5.4, 7.4, 5.4]
}


df = pd.DataFrame(D)

res = df.groupby('Name', as_index=False).mean()
res.columns = ['Name', 'AVG Measurement value']

print(res)

Prints

  Name  AVG Measurement value
0  PN1               5.400000
1  PN2               5.766667
2  PN3               9.600000
3  PN4               7.400000


For plotting, you can use matplotlib:

import pandas as pd
import matplotlib.pyplot as plt


D = {
    'Name': ['PN1', 'PN2', 'PN3', 'PN2', 'PN4', 'PN2'],
    'Measurement value': [5.4, 6.5, 9.6, 5.4, 7.4, 5.4]
}


df = pd.DataFrame(D)


res = df.groupby('Name', as_index=False).mean()
res.columns = ['Name', 'AVG Measurement value']


plt.figure(figsize=(8, 5))
plt.bar(res['Name'], res['AVG Measurement value'], color='black')
plt.xlabel('Name')
plt.ylabel('AVG')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()


enter image description here

like image 53
Aicody Avatar answered Mar 07 '26 02:03

Aicody