Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string of numbers and letters to int/float in pandas dataframe

Tags:

python

pandas

I feel like there has to be a quick solution to my problem, I hacked out a poorly implemented solution using multiple list comprehensions which is not ideal whatsoever. Maybe someone could help out here.

I have a set of values which are strings (e.g. 3.2B, 1.5M, 1.1T) where naturally the last character denotes million, billion, trillion. Within the set there are also NaN/'none' values which should remain untouched. I wish to convert these to floats or ints, so in the given example (3200000000, 1500000, 1100000000000)

TIA

like image 996
ast4 Avatar asked Jan 08 '13 15:01

ast4


3 Answers

You could create a function: and applymap it to every entry in the dataframe:

powers = {'B': 10 ** 9, 'M': 10 ** 6, 'T': 10 ** 12}
# add some more to powers as necessary

def f(s):
    try:
        power = s[-1]
        return int(s[:-1]) * powers[power]
    except TypeError:
        return s

df.applymap(f)
like image 73
Andy Hayden Avatar answered Sep 19 '22 23:09

Andy Hayden


Setup
Borrowing @MaxU's pd.DataFrame

df = pd.DataFrame({'col': ['123.456', '78M', '0.5B']})

Solution
Replace strings with scientific notation then use astype(float)

d = dict(M='E6', B='E9', T='E12')

df.replace(d, regex=True).astype(float)

            col
0  1.234560e+02
1  7.800000e+07
2  5.000000e+08
like image 21
piRSquared Avatar answered Sep 21 '22 23:09

piRSquared


Demo:

In [58]: df
Out[58]:
       col
0  123.456
1      78M
2     0.5B

In [59]: d = {'B': 10**9, 'M': 10**6}

In [60]: df['new'] = \
    ...: df['col'].str.extract(r'(?P<val>[\d.]+)\s*?(?P<mult>\D*)', expand=True) \
    ...:   .replace('','1') \
    ...:   .replace(d, regex=True) \
    ...:   .astype(float) \
    ...:   .eval('val * mult')
    ...:

In [61]: df
Out[61]:
       col           new
0  123.456  1.234560e+02
1      78M  7.800000e+07
2     0.5B  5.000000e+08
like image 34
MaxU - stop WAR against UA Avatar answered Sep 20 '22 23:09

MaxU - stop WAR against UA