New to python. Looking to find a mean of 1 column if the 2nd column is below a specific number. So let's say I have a data frame with 4 columns, 2 of which are age and salary. I would like to find out average salary for people below 40 and for people above 40.
Thanks in advance
CodePudding user response:
mean_people_below_40 = df.loc[(df.age < 40)].salary.mean()
mean_people_above_40 = df.loc[(df.age > 40)].salary.mean()
CodePudding user response:
It calculates the mean of the ‘X’ column for every row in the DataFrame where the ‘Y’ column is equal to ‘The given Condition’.
import pandas as pd
df = pd.DataFrame({'class': ['A', 'A', 'A', 'B', 'B', 'B'],
'full_marks': [99, 90, 93, 86, 88, 82],
'marks_obtained': [33, 28, 31, 39, 34, 30]}) print(df)
result = df.loc[df['class'] == 'A', 'marks_obtained'].mean()
print("The mean is: ", result)
Hope this helps
CodePudding user response:
df = pd.DataFrame({'Nama': ['A', 'B','C','D','E'],
'Tugas': [20, 28, 10, 34, 77]})
result = df.loc[(df.Tugas < 40)].Tugas.mean()
result1 = df.loc[(df.Tugas > 40)].Tugas.mean()