Luck_R
Luck_R

Reputation: 35

'DataFrame' object has no attribute 'str' - .str.replace error

I am trying to replace "," by "" for 80 columns in a panda dataframe. I have create a list of the headers to iterate:

headers = ['h1', 'h2', 'h3'... 'h80']

and then I am using the list to replace multiple columns string value as bellow:

dataFrame[headers] = dataFrame[headers].str.replace(',', '')

Which gave me this error: AttributeError: 'DataFrame' object has no attribute 'str'

When I try the same on only one header it works well, and I need to use the str.replace because the only replace method does sadly not replace the ",".

Upvotes: 3

Views: 11114

Answers (1)

Akshay Sehgal
Akshay Sehgal

Reputation: 19312

Using df.apply

pd.Series.str.replace is a series method not for dataframes. You can use apply on each column (series) instead.

dataFrame[headers] = dataFrame[headers].apply(lambda x: x.str.replace(',', ''))

Another option is to use apply on each row (series) with axis=1.

Using df.applymap

Or, you can use applymap and treat each cell as a string and use replace directly on them.

dataFrame[headers] = dataFrame[headers].applymap(lambda x: x.replace(',', ''))

Using df.replace

You can also use df.replace which is a method available to replace values in df directly across all columns selected. But, for this purpose you will have to set regex=True.

dataFrame[headers] = dataFrame[headers].replace(',', '', regex=True)

Upvotes: 9

Related Questions