Simone Panico
Simone Panico

Reputation: 115

Removing units of measurement from a database (python)

enter image description here

I have a database that looks like the picture. I would like to remove all units of measurement. Some columns have writing on them is there any way I can do this?

3   390 kg/m3   1081 J/kgK  0.1 W/mK
4   420 kg/m3   1081 J/kgK  0.112 W/mK
5   600 kg/m3   1081 J/kgK  0.21 W/mK
6   2009.88 kg/m3   843.584 J/kgK   1.01233 W/mK
7   1674.2 kg/m3    933 J/kgK   0.685 W/mK

Upvotes: -1

Views: 2199

Answers (2)

MEcoder
MEcoder

Reputation: 19

For float values, you'll want to make sure the regular expression considers both before and after the decimal point and change the datatype:

df['col'] = df['col'].str.extract('(\d+\.\d+)').astype(float)

Upvotes: 0

gtomer
gtomer

Reputation: 6564

You can clean the strings and leave only the digits. For example:

df['col'] =  df['col'].str.extract('(\d+)').astype(int)

Upvotes: 3

Related Questions