user2629628
user2629628

Reputation: 141

Iterate over pandas series gives

Basically I have this pd.series:

0         03/25/93 Total time of visit (in minutes):\n
1                       6/18/85 Primary Care Doctor:\n
2    sshe plans to move as of 7/8/71 In-Home Servic...
3                7 on 9/27/75 Audit C Score Current:\n
4    2/6/96 sleep studyPain Treatment Pain Level (N...

When I try to iterate over it with a loop:

  for i,row in enumerate(df):
    d= row[i].len()

Or this:

    for row in df:
        d= row.len()

I get this error:

AttributeError: 'str' object has no attribute 'len'

Also receive this error message when I try other operations like findall etc.

Hope somebody can enlighten me! Thanks.

Upvotes: 0

Views: 54

Answers (1)

Sy Ker
Sy Ker

Reputation: 2190

You have to use .str to access string functionalities in a Series and you don't need to iterate over every rows.

This will do;

df['str_len'] = df['str_column'].str.len()

By the way in pandas, .findall is .str.contains which returns a boolean indexer.

Use it like this;

substring = 'hello'

df[df['str_column'].str.contains(substring)]

Upvotes: 1

Related Questions