0

I have a dataframe like the following (example data given):

df = pd.DataFrame({'smiles': ['CCCCC', 'CCCC1', 'CCCN1'],
                        'ID' : ['A-111', 'A112', 'A-113'],
              'Parameter_1':[30.0, 31.4, 15.9],
              'Parameter_2':[NaN, '0.644', '4.38E-02'],
              'Date': [dt.date(2021, 1, 1), dt.date(2021, 1, 2), dt.date(2021, 1, 3)]})

I have the following function:

def num_parse(element):
    try:
        float(element)
        return float(element)
    except ValueError:
        return(element)
    except TypeError:
        return(element)

When I apply my function to individual columns it works fine - converting any string that can be floated into a float and leaving all other strings as is and also leaving the datetime column as is.

df['Parameter_1'] = df['Parameter_1'].apply(num_parse)

When I apply this to my entire dataframe I keep getting the following error:

df = df.apply(num_parse)

TypeError: cannot convert the series to <class 'float'>

I am unsure why, please help.

Stuchfield
  • 53
  • 4
  • How do you apply it? `df.applymap(num_parse)` should work – mozway Nov 22 '22 at 14:29
  • I misread initially but added how I applied - I was just using df.apply(num_parse) not the method you suggested which does appear to work many thanks! – Stuchfield Nov 22 '22 at 14:34
  • You're welcome, `applymap` applies per "cell", while `apply` works per "column", and your function is not vectorized (i.e. cannot accept a Series) – mozway Nov 22 '22 at 14:37

1 Answers1

2

Use applymap()

df.applymap(num_parse)

You could also:

df.apply(num_parse, axis=1)
greenButMellow
  • 316
  • 1
  • 9
  • This appears to work thank you! How does .applymap() and .apply(), differ? What makes them act on the dataframe differently? – Stuchfield Nov 22 '22 at 14:35
  • https://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas – mtm1186 Nov 22 '22 at 14:41