带join的groupby agg无法产生预期的输出 [英] Groupby agg with join not produce the expected output
问题描述
我的数据框如下
Wash_Month Wash_Day
0 3 2
1 4 3
预期的产量是
#d={'Wash_Month':'Wash_Month/Wash_Day','Wash_Day':'Wash_Month/Wash_Day'}
#df.T.astype(str).groupby(d).agg(','.join)
Out[329]:
0 1
Wash_Month/Wash_Day 3,2 4,3
如您所见,我首先进行转置T
.
As you saw , I first do the transpose T
.
如果我们groupby
与axis=1
并删除了T
,我希望得到同样的输出结果.
If we groupby
with axis=1
and remove the T
, I expected the same out put.
df.astype(str).groupby(d,axis=1).agg(','.join)
Out[330]:
Wash_Month/Wash_Day
0 Wash_Month,Wash_Day
1 Wash_Month,Wash_Day
输出与预期输出不匹配.在agg
和join
以及axis=1
的groupby
上是否存在特定问题
The out put is mismatched with expected output . Is there specific problem onagg
with join
with groupby
of axis=1
由于sum
之类的其他agg
功能正常运行
Since other agg
function like sum
work as normal
df.astype(str).groupby({'Wash_Month':'Wash_Month/Wash_Day','Wash_Day':'Wash_Month/Wash_Day'}, axis=1).sum()
Out[332]:
Wash_Month/Wash_Day
0 32.0 # str 3 + str 2
1 43.0
关于为什么结果变为浮动而不是str检查的原因链接
About why the result become float rather than a str check link
感谢您的帮助:-)
推荐答案
以下是提示:
def f(x):
print(x)
print(type(x))
return 1
df.astype(str).groupby(d,axis=1).agg(f)
输出:
Wash_Month Wash_Day
0 3 2
1 4 3
<class 'pandas.core.frame.DataFrame'>
请注意输出是一个数据框.
Note the output is a dataframe.
相对于:
def f(x):
print(x)
print(type(x))
return 1
df.T.astype(str).groupby(d).agg(f)
输出:
Wash_Month 3
Wash_Day 2
Name: 0, dtype: object
<class 'pandas.core.series.Series'>
Wash_Month 4
Wash_Day 3
Name: 1, dtype: object
<class 'pandas.core.series.Series'>
每个系列都调用f,因此"join"将列标题串联起来.
Which f gets called with each series, hence 'join' is concatenating the column headers.
我无法通过解释源代码来解释它,但是看来groupby和astype(str)会导致agg在每种情况下的动作都不同.
I can't explain it with digging through the source code, but it appears that the groupby along with astype(str) is causing agg to act differently in each situation.
这篇关于带join的groupby agg无法产生预期的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!