简体   繁体   中英

Using Pandas to Extract From a .csv File

I am attempting to answer the following question given a csv file with columns corresponding to final exam score and gender (among others). I understand how to solve using a for loop, but not pandas.

"Find the mean Final exam score for all females in the class."

import pandas as pd 
grades = pd.read_csv('samplegrades.csv',index_col=0)
grades['Final','Gender'== 'F'].mean() #Incorrect Line

I would appreciate any suggestions on correct syntax using pandas. Thanks

尝试,

grades.loc[grades['Gender'] == 'F', 'Final'].mean()

Option 1 is given by @Vaishali
Most idiomatic answer.


Option 2

grades.query('Gender = "F"').Final.mean()

Option 3

grades.groupby('Gender').Final.mean().loc['F']

Option 4

grades[df.Gender.eq('F')].Final.mean()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM