1

Below is script for a simplified version of the df in question:

import pandas as pd

df = pd.DataFrame({ 
               'feature'       : ['cd_player', 'sat_nav', 'sub_woofer', 'usb_port','cd_player', 'sat_nav', 'sub_woofer', 'usb_port','cd_player', 'sat_nav', 'sub_woofer', 'usb_port'],
               'feature_value' : ['1','1','0','4','1','0','0','1','1','1','1','0'],
               'feature_colour' : ['red','orange','yellow','green','blue','indigo','violet','red','orange','yellow','green','blue']
                 })
df
    feature     feature_value   feature_colour
0   cd_player   1               red
1   sat_nav     1               orange
2   sub_woofer  0               yellow
3   usb_port    4               green
4   cd_player   1               blue
5   sat_nav     0               indigo
6   sub_woofer  0               violet
7   usb_port    1               red
8   cd_player   1               orange
9   sat_nav     1               yellow
10  sub_woofer  1               green
11  usb_port    0               blue

df.dtypes

feature          object
feature_value    object
dtype: object

I want to find a way to find all columns with numerical values, and convert their datatypes to integers and/or floats. Of course in this example, it is easy to do manually, however the DF in question has ~50 potential cols with numerical values, but as they are all have object dtypes, it would be rather inefficient to determine manually.

INTENDED OUTPUT:

df.dtypes

feature          object
feature_value     int64
dtype: object

Any help would be greatly appreciated.

1 Answer 1

4

Try this:

df = df.apply(lambda x: pd.to_numeric(x, errors='ignore'))
df.dtypes
Sign up to request clarification or add additional context in comments.

1 Comment

It worked just as I had hoped, many thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.