Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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 ",".

like image 580
Luck_R Avatar asked Jun 22 '26 19:06

Luck_R


1 Answers

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)
like image 117
Akshay Sehgal Avatar answered Jun 24 '26 07:06

Akshay Sehgal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!