1

I am unable to remove scientific notation from a column containing both string and non-string entries.

When I import a dataset using read_csv, one of the column shows the following:

0           1.9157E+11
11          1.9157E+11
12          1.9157E+11
13          1.9157E+11
14          1.9157E+11
         ...      
37444    19156794-1005
37445    19156794-1004
37446      19156791023
37447    19156794-1003
37448    19156794-1003

While all of the entries are encoded as strings, some rows contain only numbers (hence they are converted to scientific notation) and some rows contain "-" (hence they are displayed properly).

When I print dataframe['column'][11], I get '1.9157E+11', which is a string.

I am unable to use apply(float) to the whole column because some of the entries contain '-', and therefore cannot be converted to string.

There was this question asked as well but the OP faced the same problem (see comments under solution) and no resolution was achieved.

johnconnor92
  • 199
  • 1
  • 13

1 Answers1

1

You can use the following code, if you need to convert all the convertable values and leave others as is:

def try_float(x):
    try:
        return float(x)
    except ValueError as e:
        return x

series.apply(try_float)
koPytok
  • 3,453
  • 1
  • 14
  • 29
  • Is there a way to write this in lambda function? Just curious – johnconnor92 Jun 13 '18 at 10:15
  • @johnconnor92 No, but [that](https://stackoverflow.com/questions/12451531/python-try-catch-block-inside-lambda) may help you – koPytok Jun 13 '18 at 10:18
  • Alright, thank you! Problem solved. I am not familiar with using 'except' clause yet, that's why I don't know how to write this function. – johnconnor92 Jun 13 '18 at 10:24
  • @johnconnor92 try-catch syntax is not hard. The function runs `try` code, and in case it gets `ValueError`, it runs `except ValueError` code. – koPytok Jun 13 '18 at 10:28