将列替换为特定字符的计数

时间:2017-04-12 08:17:01

标签: python pandas numpy

我有一个包含以下列的数据框:

1        2
2        3

我想遍历孔列并将值替换为“;”获得+1(字符串数):

"[2015] Google Nexus Project"
"2.4GHz Antenna"
"AC Study Dev 2GHz MMIC blocks"
2ndlineTAM
"8D GaN Rth fails after temp stress tests"
90NV Dual Source
"TFA9890R RDL ASEK"
"93k platform introduction BU ID"

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用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