【问题标题】:Find the average of a column that meets criteria of other columns查找满足其他列条件的列的平均值
【发布时间】:2022-11-15 07:04:14
【问题描述】:

我有一个数据集,我需要使用 Python 和 Pandas 在满足特定条件的列中查找特定项目的平均价格。标准是“制造商”栏中的“本田”和“丰田”,“条件”栏中的“良好”,以及“类型”栏中的“轿车”。价格在“价格”栏

我知道我需要使用 groupby() 但我不确定还能做什么

import numpy as np 
import pandas as pd 
df = pd.read_csv("vehicles.csv")
def get_avg_prices():
df.groupby(['criteria','manufacturer','condition']).['price'].mean

这就是我开始的方式,但不确定从这里去哪里或者这是否正确

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    df.groupby(['criteria','manufacturer','condition'])['price'].mean()
    

    这就是正确的 Groupby 函数。你有一个额外的“。” ['price'] 之前的点,您缺少一对括号 ()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 2021-05-12
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      相关资源
      最近更新 更多