我有一个带有以下关键字的词典列表:国家,点,价格。我需要获得每个国家/地区的平均积分和价格。这是列表:
0: {country: "US", points: 96, price: 235}
1: {country: "Spain", points: 96, price: 110}
2: {country: "US", points: 96, price: 90}
3: {country: "US", points: 96, price: 65}
我需要一列包含国家及其平均值的字典。
我已经到达了一个目录,其中列出了价格和积分之和:
[{'country': 'Albania', 'points': 176, 'price': 40.0}, {'country': 'Argentina', 'points': 480488, 'price': 116181.0}, {'country': 'Australia', 'points': 430092, 'price': 152979.0}
现在,我需要获取平均值。我正在考虑为country.length创建另一个密钥,然后在for循环中执行基本计算。但是不确定这是否是正确的方法...谢谢您的帮助!
我的下面的代码:
count_dict = country_count.to_dict()
# Output
{'US': 62139,
'Italy': 18784,
'France': 14785,
'Spain': 8160}
# Get the sum of points and price for each country
grouped_data = wine_data.groupby('country').agg({'points':'sum', 'price':'sum'})
# Reset the index in order to convert df into a list of dictionaries
country_data = grouped_data.reset_index()
country_list = country_data.to_dict('records')
# Output
[{'country': 'Albania', 'points': 176, 'price': 40.0}, {'country': 'Argentina', 'points': 48048 etc]```
答案 0 :(得分:0)
您是否尝试过将数据传递到Pandas DataFrame并在那里使用它?
您可以执行以下操作,首先创建一个DataFrame:
import pandas as pd
import numpy as np
d = {
0: {'country': "US", 'points': 96, 'price': 235},
1: {'country': "Spain", 'points': 96, 'price': 110},
2: {'country': "US", 'points': 96, 'price': 90},
3: {'country': "US", 'points': 96, 'price': 65}
}
#
df = pd.DataFrame(d).Transpose()
Out:
country points price
0 US 96 235
1 Spain 96 110
2 US 96 90
3 US 96 65
然后是groupby
个国家/地区
# just to make sure they are numeric
df[['points','price']] = df[['points','price']].astype('float64')
df.groupby('country').mean()
Out:
points price
country
Spain 96.0 110.0
US 96.0 130.0