Python dataframe groupby列错误无应用成员
我使用pandas获得数据集的平均价格和总数量。 代码正常,但我收到以下错误消息: “DataFrameGroupBy”的实例没有行的“apply”成员 摘要报告=df.groupby('name')。应用(f1) 您知道如何解决此错误消息吗 我正在使用VisualStudio代码。已经在使用最新版本。有关错误消息,请参见下面的屏幕截图。使用panda版本1.1.1Python dataframe groupby列错误无应用成员,python,pandas,Python,Pandas,我使用pandas获得数据集的平均价格和总数量。 代码正常,但我收到以下错误消息: “DataFrameGroupBy”的实例没有行的“apply”成员 摘要报告=df.groupby('name')。应用(f1) 您知道如何解决此错误消息吗 我正在使用VisualStudio代码。已经在使用最新版本。有关错误消息,请参见下面的屏幕截图。使用panda版本1.1.1 代码对我来说运行得非常好。但可能存在一些潜在问题- 您已将groupby用作变量名,并覆盖了pandas方法。请重新启动内核并再次
代码对我来说运行得非常好。但可能存在一些潜在问题-
pip安装-升级pandas
将您的pandas更新为稳定版本你在哪里运行这个代码?我没有收到任何错误,您的代码工作正常。我找不到任何错误!在Windows10的panda版本1.1.1中,使用python版本3.8.5 64位从VisualStudio代码运行此代码。
import pandas as pd
dataset = [
{'name': 'A', 'Quantity': 37, 'Price': 10},
{'name': 'B', 'Quantity': 20, 'Price': 10.5},
{'name': 'A', 'Quantity': 17, 'Price': 9},
{'name': 'D', 'Quantity': 19, 'Price': 5},
{'name': 'E', 'Quantity': 30, 'Price': 6}
]
def f1(x):
d={}
d['Total_Quantity']=x['Quantity'].sum()
d['Average_Price']= ((x['Quantity'] * x['Price']).sum())/x['Quantity'].sum()
return pd.Series(d, index=['Total_Quantity', 'Average_Price'])
df = pd.DataFrame(dataset)
summary_Report = df.groupby('name').apply(f1)
print(summary_Report)
import pandas as pd
dataset = [
{'name': 'A', 'Quantity': 37, 'Price': 10},
{'name': 'B', 'Quantity': 20, 'Price': 10.5},
{'name': 'A', 'Quantity': 17, 'Price': 9},
{'name': 'D', 'Quantity': 19, 'Price': 5},
{'name': 'E', 'Quantity': 30, 'Price': 6}
]
def f1(x):
d={}
d['Total_Quantity']=x['Quantity'].sum()
d['Average_Price']= ((x['Quantity'] * x['Price']).sum())/x['Quantity'].sum()
return pd.Series(d, index=['Total_Quantity', 'Average_Price'])
df = pd.DataFrame(dataset)
summary_Report = df.groupby('name').apply(f1)
print(summary_Report)
Total_Quantity Average_Price
name
A 54.0 9.685185
B 20.0 10.500000
D 19.0 5.000000
E 30.0 6.000000