如何在 pandas 中获取使用另一列的值选择的行的平均值 [英] How to get mean of rows selected with another column's values in pandas
问题描述
仅当列Dates
等于Oct-16
时,我才试图计算得分1的平均值:
I am trying to get calculate the mean for Score 1 only if column Dates
is equal to Oct-16
:
我最初尝试的是:
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())
但是我的结果是整列Score 1
我尝试的另一件事是手动告诉它要计算哪些指数的平均值:
Another thing I tried was manually telling it which indices to calculate the mean for:
dataFrame["Score 1"].iloc[0:2].mean()
但是理想情况下,如果Dates == "Oct-16"
,我想找到一种方法.
But ideally I would like to find a way to do it if Dates == "Oct-16"
.
推荐答案
遍历行并没有利用Pandas的优势.如果要基于另一列的值对某列进行操作,则可以使用
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']
.loc[]
的第一部分使用指定的条件(dataFrame['Dates'] == 'Oct-16'
)选择所需的行.第二部分指定所需的列(Score 1
).然后,如果您想获取均值,则可以在末尾放置.mean()
:
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()
这篇关于如何在 pandas 中获取使用另一列的值选择的行的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!