[英]Pandas pivot_table() aggfunc aggregation conditional on multiple columns?
我想用 Pandas 数据透视表聚合一列,但自定义聚合应该以数据帧中的不同列为条件。
请参见下面的示例:假设如果“Number_mentions”的值高于阈值,我想为“Newspaper”列中的每个值对“Number_mentions”列求和。 使用自定义 aggfunc 很容易做到这一点。 但是,此外,如果我只想对与“国家/地区”列中的值“RU”不在同一行的那些“Number_mentions”求和,该怎么办? 似乎 aggfunc 只能将一列与其他列隔离开来,我不知道如何将整个数据帧放入 aggfunc 中以在 aggfunc 中进行条件子集化。
df = pd.DataFrame({"Number_mentions": [1,5,2,3,6,5],
"Newspaper": ["Newspaper1", "Newspaper1", "Newspaper2", "Newspaper3", "Newspaper4", "Newspaper5"],
"Country": ["US", "US", "CN", "CN", "RU", "RU"]})
def articles_above_thresh_with_condition(input_series, thresh=2):
series_bool = input_series > thresh
# ! add some if condition based on additional column in df:
# ! only aggregate those values where column "Country" is not "RU".
# ? code ?
n_articles_above_thresh = sum(series_bool)
return n_articles_above_thresh
df_piv = pd.pivot_table(df, values=["Number_mentions"],
index="Newspaper", columns=None, margins=False,
aggfunc=articles_above_thresh_with_condition)
您需要不同的方法,因为 pivot_table 不能处理 2 列。
因此,首先通过Series.where
将不匹配的值替换为缺失值,然后处理这个新列:
df["Number_mentions1"] = df["Number_mentions"].where(df["Country"].ne('RU'))
print (df)
Number_mentions Newspaper Country Number_mentions1
0 1 Newspaper1 US 1.0
1 5 Newspaper1 US 5.0
2 2 Newspaper2 CN 2.0
3 3 Newspaper3 CN 3.0
4 6 Newspaper4 RU NaN
5 5 Newspaper5 RU NaN
df_piv = pd.pivot_table(df, values=["Number_mentions1"],
index="Newspaper", columns=None, margins=False,
aggfunc=articles_above_thresh_with_condition)
print (df_piv)
Number_mentions1
Newspaper
Newspaper1 1.0
Newspaper2 0.0
Newspaper3 1.0
Newspaper4 0.0
Newspaper5 0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.