Home > Software engineering >  Iterate over columns and rows in a pandas dataframe and convert string to float
Iterate over columns and rows in a pandas dataframe and convert string to float

Time:12-10

I have the following dataframe:

col1  col2  col3
25,4  34,2  33,2
33,25 30.2  10,2
.................

and I want to iterate all over the columns and rows from this dataset.

df_range = len(df)

for column in df:
  for i in range(df_range):
    str.replace(',', '.').astype(float)
    print(df)

and I get the following error:

TypeError                                 Traceback (most recent call last)

<ipython-input-38-47f6c96d2e67> in <module>()
      3 for column in df2:
      4   for i in range(df_range):
----> 5     str.replace(',', '.').astype(float)
      6 
      7     print(df)

TypeError: replace() takes at least 2 arguments (1 given)

CodePudding user response:

Why would str.replace(',', '.').astype(float) give you anything useful? There's nothing in that expression that involves the thing you're iterating over. Even if it were to evaluate to something without an error, it would evaluate to the same thing in each iteration of the loop.

If you do df.loc[i,column].replace(',','.'), then replace is a method from the string object df.loc[i,column], and takes two arguments old and new. However, when you do str.replace(',','.'), replace is a method from the str type, not from a string instance, and so requires the arguments self old and new. The first argument ',' gets interpreted as self, and that leaves '.' as old and nothing for new. When you use replace, you have to either give it the original string as an argument, or take the replace method from the original string.

Also, you shouldn't be iterating through df with indices. Us applymap instead.

CodePudding user response:

Assuming you want to change commas to dots across all rows and columns, you should do:

df = df.applymap(lambda x: x.replace(',','.')).astype(float)

For specific columns, you can do:

df['col1'] = df['col1'].str.replace(',','.').astype(float)

or

df['col1'] = df['col1'].map(lambda x: x.replace(',','.')).astype(float)

or

df['col1'] = df['col1'].apply(lambda x: x.replace(',','.')).astype(float)
  • Related