Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Data wrangling: strings to numbers

Many datasets have numerical values encoded in strings which need to be converted intonumbers for analysis

data = '40k'
data.split('k')
['40', '']
int(data.split('k')[0])*1000
40000
datalist = ['40k', '31k', '12k']

For use with Table columns or other array data

from datascience import *
import numpy as np
from datascience import *
t = Table().with_columns('index',[0,1,2],'amount',datalist)
t
Loading...
t = t.with_columns('value',[int(data.split('k')[0])*1000 for data in datalist])
t
Loading...

Float data embeded in string within table

datalist = ['4.01k', '3.11k', '1.25k']
[float(data.split('k')[0])*1000 for data in datalist]
[4010.0, 3110.0, 1250.0]
salary = Table().with_columns('position',['Data scientist','Chemist','Chemist','Biologist','Physicist','Finance'],
                              'salary',['75k','102k','99k','103k','99k','34k'])
salary
Loading...
amount = [int(data.replace(',','').split('k')[0])*1000 for data in salary.column('salary')]
amount
[75000, 102000, 99000, 103000, 99000, 34000]
salary = salary.with_columns('salary',amount)
salary
Loading...

Working with time strings

time_values = ['12:03:56', '01:04:23', '03:35:00']
t = t.with_columns('time',time_values,'hour',[int(data.split(':')[0]) for data in time_values])
t
Loading...