提问人:charancherry 提问时间:2/23/2023 最后编辑:jezraelcharancherry 更新时间:2/23/2023 访问量:205
FutureWarning:不推荐使用多个键(隐式转换为键元组)的索引,请改用列表 [duplicate]
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead [duplicate]
问:
我创建了一个数据帧
team3 team4 Dummy
0 F YG 10
1 g Gh 10
2 h Ya 10
3 i nG 10
4 k Yb 10
5 l Yf 10
6 m jk 10
我正在尝试获得预期的输出,如下所示
team3 team4 Dummy
0 F|g|h|i|k|l|m YG|gh|ya|ng|yb|yf|jk 10
df10 = df9.groupby('Dummy')['team3','team4'].apply('|'.join).reset_index()
收到错误:
FutureWarning:不推荐使用多个键(隐式转换为键元组)的索引,请改用列表。 df10 = df9.groupby('虚拟')['team3','team4'].apply('|'.加入).reset_index()
答:
0赞
jezrael
2/23/2023
#1
添加用于选择多个列的嵌套列表,而不是 GroupBy.apply
,使用 GroupBy.agg
分别处理每个列:
#joined columns names
print (df9.groupby('Dummy')[['team3','team4']].apply('|'.join).reset_index())
Dummy 0
0 10 team3|team4
df10 = df9.groupby('Dummy')[['team3','team4']].agg('|'.join).reset_index()
print (df10)
Dummy team3 team4
0 10 F|g|h|i|k|l|m YG|Gh|Ya|nG|Yb|Yf|jk
如果顺序很重要:
df10 = df10[['team3','team4','Dummy']]
print (df10)
team3 team4 Dummy
0 F|g|h|i|k|l|m YG|Gh|Ya|nG|Yb|Yf|jk 10
评论