[英]list mean values of all possible column combinations
我有一个看起来像这样的数据集:
Value Type country mean
-1.975767 Weather Brazil
-0.540979 Fruits China
-2.359127 Fruits China
-2.815604 Corona China
-0.712323 Weather UK
-0.929755 Weather Brazil
我想计算类型和国家/地区的所有不同组合的总体平均值。 例如:
巴西天气的平均值 = (-1.975767 -0.929755) / 2
然后我想将这些组合添加到另一个表中:
df2 = pd.DataFrame()
country type mean count
Brazil Weather 2
Brazil Corona
China Corona 1
China Fruits 2
我可以这样计算平均值:
print(df.groupby(["type", "country"])["value"].mean())
但是如何以所需格式将这些值保存在新的 df 中?
编辑:这有效
df_new = df.groupby(["type", "country"], as_index = False)["value"].mean()
但是如果我尝试以相同的方式添加一个计数:
df_new = df.groupby(["type", "country"], as_index = False).count()
它转置所有列而不是在平均列之后添加计数列
您可以在groupby
中使用as_index
参数:
df_new = df.groupby(["type", "country"], as_index = False)["value"].mean()
那么结果就是一个标准的dataframe:
type country value
0 Corona China -2.815604
1 Fruits China -1.450053
2 Weather Brazil -1.452761
3 Weather UK -0.712323
编辑:我们如何添加另一列count
? 你可以简单地 append 一个新的列与一个新的groupby
的结果是这样的:
# original answer
df_new = df.groupby(["type", "country"], as_index = False)["value"].mean().rename(columns={'value':'mean'})
# Add count also
df_new['count'] = df.groupby(["type", "country"])["value"].count().tolist()
df_new
Output:
type country mean count
0 Corona China -2.815604 1
1 Fruits China -1.450053 2
2 Weather Brazil -1.452761 2
3 Weather UK -0.712323 1
就个人而言,我更喜欢 JANO 的回答,但这是我想出的:
import pandas as pd
dataframe = pd.DataFrame({"Value":[-1.23, -1.65, -0.123, -0.67, 2.456], "Type":["Weather", "Fruits", "Corona", "Corona", "Weather"], "country": ["Brazil", "China", "China", "Iran", "Iran"]})
resultDataframe = {"Value":[], "Type":[], "country":[]}
for country in dataframe["country"].unique():
tempDataframe = dataframe[dataframe["country"] == country]
a = tempDataframe.groupby(by="Type").mean().reset_index()
for index, row in a.iterrows():
resultDataframe["Value"].append(row["Value"])
resultDataframe["Type"].append(row["Type"])
resultDataframe["country"].append(country)
pd.DataFrame(resultDataframe)
价值 | 类型 | 国家 | |
---|---|---|---|
0 | -1.23 | 天气 | 巴西 |
1个 | -0.123 | 电晕 | 中国 |
2个 | -1.65 | 水果 | 中国 |
3个 | -0.67 | 电晕 | 伊朗 |
4个 | 2.456 | 天气 | 伊朗 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.