Home > OS >  How to find the count of spaces in a dataframe using python
How to find the count of spaces in a dataframe using python

Time:09-30

I have an excel file which has few columns, I need to find no.of spaces in the column values.

Example:

Key
Identifier
Identifier Number
user identifier number
user
Location of user
  df['count'] =df['Key'].apply(lambda x:x.isspace())

Above line is giving True, False values. Can anyone help me how to get the count of no.of spaces in 'key' column name.

CodePudding user response:

Use Series.str.count with \s for match spaces:

df['count'] =df['Key'].str.count('\s ')
print (df)
                      Key  count
0              Identifier      0
1       Identifier Number      1
2  user identifier number      2
3                    user      0
4        Location of user      2
  • Related