3

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

0

1 Answer 1

9

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.