我有一个数据框,其中包含如下列:
1 string;string
2 string;string;string
我想迭代孔列并将值替换为“;”的计数+1(字符串数)得到:
1 2
2 3
感谢您的帮助。
最佳答案
您可以使用str.count
功能:
print (df)
col
1 string;string
2 string;string;string
df['col'] = df['col'].str.count(';') + 1
print (df)
col
1 2
2 3
<小时/>
df['col'] = df['col'].str.count(';').add(1)
print (df)
col
1 2
2 3
关于python - 将列替换为特定字符的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43363991/