4

I am trying to get calculate the mean for Score 1 only if column Dates is equal to Oct-16:

enter image description here

What I originally tried was:

 import pandas as pd
 import numpy as np
 import os

 dataFrame = pd.read_csv("test.csv")

 for date in dataFrame["Dates"]:
    if date == "Oct-16":
        print(date)##Just checking
        print(dataFrame["Score 1"].mean())

But my results are the mean for the whole column Score 1

Another thing I tried was manually telling it which indices to calculate the mean for:

dataFrame["Score 1"].iloc[0:2].mean()

But ideally I would like to find a way to do it if Dates == "Oct-16".

ASGM
  • 11,051
  • 1
  • 32
  • 53
Quinnystar27
  • 322
  • 1
  • 4
  • 14

3 Answers3

7

Iterating through the rows doesn't take advantage of Pandas' strengths. If you want to do something with a column based on values of another column, you can use .loc[]:

dataFrame.loc[dataFrame['Dates'] == 'Oct-16', 'Score 1']

The first part of .loc[] selects the rows you want, using your specified criteria (dataFrame['Dates'] == 'Oct-16'). The second part specifies the column you want (Score 1). Then if you want to get the mean, you can just put .mean() on the end:

dataFrame.loc[dataFrame['Dates'] == 'Oct-16', 'Score 1'].mean()
ASGM
  • 11,051
  • 1
  • 32
  • 53
2

How about the mean for all dates

dataframe.groupby('Dates').['Score 1'].mean()
piRSquared
  • 285,575
  • 57
  • 475
  • 624
1
import pandas as pd
import numpy as np
import os

dataFrame = pd.read_csv("test.csv")

dates = dataFrame["Dates"]
score1s = dataFrame["Score 1"]
result = []

for i in range(0,len(dates)):
    if dates[i] == "Oct-16":
        result.append(score1s[i])

print(result.mean())
Raza
  • 205
  • 1
  • 8