How to get mean of rows selected with another column's values in pandas


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

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".


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()
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())

How about the mean for all dates

dataframe.groupby('Dates').['Score 1'].mean()



  • Wordpress ecommerce plugin allowing client to upload image [closed]
  • How to use Ignore globally in AutoMapper?
  • Compare two table and find matching columns
  • Full stack trace for intercepted methods at call site in Unity
  • Slf4j with Log4j does not print wrapped exception (caused by) when wrapper exception has a message
  • Wordpress form with file upload submit with Ajax
  • Where is the buffer allocated in opencl?
  • getsockname return invalid address
  • Sending EoS to filesink while removing branch from tee
  • Create and load data (objects)
  • Access IPV6 with IPV4 [closed]
  • how do I array from a GET request function in viewdidload function in swift
  • Errors with Codename One “Send iOS Build” and “Send Android Build”
  • Running Applescript from a Cocoa application
  • Modification of innerHTML stored in variable not working
  • How to list all Call logs from Log list including Sms and email logs also?
  • How to use CoreFoundation in QuickTime SDK for Windows?
  • MVC3 Extension for ValidatorMessage
  • Overwrite text file programmatically
  • Find corners of a rotated rectangle
  • In metro, get all inherited classes of an (abstract) class?
  • Auto send email based on the time and email address in database
  • internal javascript not works in angular2
  • Defer unused CSS
  • How to change the host IP sent in emails to new GitLab users to a publicly visible IP, not the local
  • Add checkbox dynamically using angular 2
  • How to use array in autohotkey?
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • Ajax call on Multiple selection in Select box
  • xpath assertion failure with dynamic xpath
  • how to run a different select statement based on condition in Hive SQL
  • C++ STL stack pop operation giving segmentation fault
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Angular 4: Responsive Grid List
  • Firebase: How to read from external DB?
  • Write to .csv file with PHP (Commas in Data Error)
  • convert json to excel in java