I want to count the number of instances of vertical bars "|" in a each row of a particular column in a pandas dataframe. But using str.count("|") yields some strange behaviour:
import pandas as pd
df = pd.DataFrame(['some text', None, 'a|few|vertical|bars', 'one|'])
df[0].str.count("|")
outputs
0 10.0
1 NaN
2 20.0
3 5.0
Name: 0, dtype: float64
What's going on here? If I use apply instead, I get the expected answer:
df.apply(lambda x: str(x[0]).count("|"),axis=1)
yields
0 0
1 0
2 3
3 1
dtype: int64