Home > Blockchain >  Throw and exception and move on in pandas
Throw and exception and move on in pandas

Time:07-12

I have created a pandas dataframe called df with the following code:

import numpy as np 
import pandas as pd

ds = {'col1' : ["1","2","3","A"], "col2": [45,6,7,87], "col3" : ["23","4","5","6"]}
df = pd.DataFrame(ds)

The dataframe looks like this:

print(df)

  col1  col2 col3
0    1    45   23
1    2     6    4
2    3     7    5
3    A    87    6

Now, col1 and col3 are objects:

print(df.info())

 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   col1    4 non-null      object
 1   col2    4 non-null      int64 
 2   col3    4 non-null      object

I want to transform, where possible, the object columns into floats.

For example, I can convert col3 into a float like this:

df['col3'] = df['col3'].astype(float)

But I cannot convert col1 into a float:

df['col1'] = df['col1'].astype(float)

ValueError: could not convert string to float: 'A'

Is it possible to create a code that converts, where possible, the object columns into float and by-passes the cases in which it is not possible (so, without throwing an error which stops the process)? I guess it has to do with exceptions?

CodePudding user response:

I think you can make a test wether the content in a string, object or not, in which cases the convertion won't be made. Did you try this ?

for y in df.columns:
   if(df[y].dtype == object):
      continue
   else:
      //your treatement here

or, apparetnyl in pandas 0.20.2, there is a function which makes the test : is_string_dtype(df['col1'])

--> this is in the case where all the values of a column are of the same type, if the values are mixed, iterate over df.values

CodePudding user response:

I have sorted it.

def convert_float(x):
    try:
        return x.astype(float)
    except:
        return x


cols = df.columns

for i in range(len(cols)):
    
    df[cols[i]] = convert_float(df[cols[i]])


print(df)
print(df.info())
  • Related