Reputation: 99
I am trying to count the delimiter from my CSV file using this piece of code:
import pandas as pd
df = pd.read_csv(path,sep=',')
df['comma_count'] = df.string_column.str.count(',')
print (df)
But I keep getting this error: 'DataFrame' object has no attribute 'string_column'. Trying to iterate through my dataframe had no avail.
I am trying to achieve this:
id val new comma_count
id val new 2
0 a 2.0 234.0 2
1 a 5.0 432.0 2
2 a 4.0 234.0 2
3 a 2.0 23423.0 2
4 a 9.0 324.0 2
5 a 7.0 NaN 1
6 NaN 234.0 NaN 1
7 a 6.0 NaN 1
8 4 NaN NaN 0
My file:
id,val,new
a,2,234
a,5,432
a,4,234
a,2,23423
a,9,324
a,7
,234
a,6,
4
Upvotes: 0
Views: 282
Reputation: 863681
Use different separator with select first column and count
:
df1 = pd.read_csv(path,sep='|')
df['dot_count'] = df1.iloc[:, 0].str.count(',')
Upvotes: 1