我有这样一个数据帧:
name aka fka
0 Alex Al NaN
1 Bob Bobbert Robert
2 Carol NaN NaN
我想以一系列串联的可能名称结束,例如:
0 Alex,Al
1 Bob,Bobbert,Robert
2 Carol
但是str.cat
会给我额外的,
:
df.name.str.cat([df.aka, df.fka], sep=',', na_rep='')
0 Alex,Al,
1 Bob,Bobbert,Robert
2 Carol,,
Name: name, dtype: object
我可以使用一组特殊大小写的str.replace
来确保没有前导/尾随/重复的,
,但是有没有更好的方法来连接数量可变的列呢?你知道吗
你可以用
要快速修复
str.cat
解决方案,只需添加str.strip(',')
:相关问题 更多 >
编程相关推荐