1

I have this dataFrame

df = pd.DataFrame({"A":["1","2","aj"],"B":["1.5555","899999999999999999999999","dfhasdi"]})

which gives output

    A                         B
0   1                    1.5555
1   2  899999999999999999999999
2  aj                   dfhasdi

The data Type for each cell is

               A              B
0  <class 'str'>  <class 'str'>
1  <class 'str'>  <class 'str'>
2  <class 'str'>  <class 'str'>

The required data type for each cell is

               A                B
0  <class 'int'>  <class 'float'>
1  <class 'int'>    <class 'int'>
2  <class 'str'>    <class 'str'>

I did this so far

df = df.apply(pd.to_numeric, errors='ignore')

Which doesn't change the data type.

1 Answer 1

1

Use DataFrame.applymap for elementwise check with double try-except statement if need distingush between integers, floats and strings in same column(s):

def f(x):
    try:
        return int(x)
    except Exception:
        try:
            return float(x)
        except Exception:
            return x


df = df.applymap(f)
print (df)
    A                         B
0   1                    1.5555
1   2  899999999999999999999999
2  aj                   dfhasdi

print (df.applymap(type))
               A                B
0  <class 'int'>  <class 'float'>
1  <class 'int'>    <class 'int'>
2  <class 'str'>    <class 'str'>

Close to your solution, but if mix strings with integers get floats in solution:

df = df.apply(pd.to_numeric, errors='coerce').fillna(df)
print (df)
     A                           B
0  1.0                      1.5555
1  2.0  899999999999999958056960.0
2   aj                     dfhasdi
    
print (df.applymap(type))
                 A                B
0  <class 'float'>  <class 'float'>
1  <class 'float'>  <class 'float'>
2    <class 'str'>    <class 'str'>

It working better if same types (string repr):

df = df.apply(pd.to_numeric, errors='coerce').fillna(df)
print (df)
   A                           B
0  1                      1.5555
1  2  899999999999999958056960.0
2  4                     dfhasdi

print (df.applymap(type))
               A                B
0  <class 'int'>  <class 'float'>
1  <class 'int'>  <class 'float'>
2  <class 'int'>    <class 'str'>
6
  • You can use pd.to_numeric(x, errors='ignore') in your f. Feb 15, 2021 at 14:19
  • @YevhenKuzmovych - Unfortuantely not
    – jezrael
    Feb 15, 2021 at 14:20
  • @YevhenKuzmovych - because errors='ignore' means if get error, return same values (not changed)
    – jezrael
    Feb 15, 2021 at 14:22
  • That's what you do with your exception catching, don't you? The only problem is that pd.to_numeric would return numpy's ints and floats. (Which fails on this integer that is too big for numpy's int). Apart from that, it works the same Feb 15, 2021 at 14:26
  • 1
    I'm saying that def f(x): return pd.to_numeric(x, errors='ignore') would work the same apart from having numpy's types instead of pure pythons. Feb 15, 2021 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.