我有如下数据框
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
.
If we groupby
with axis=1
并删除T
,我期望相同的输出。
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
with join
with groupby
of axis=1
由于其他agg
功能类似于sum
正常工作
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
关于为什么结果变成 float 而不是 str 检查link https://stackoverflow.com/questions/46751465/how-to-add-string-numbers-in-pandas
感谢你的帮助 :-)