How to extract or split characters from number strings using Pandas?

0 votes

Hi, guys, I've been practicing my python skills mostly on pandas and I've been facing a problem. I have a data frame selected from an SQL table that looks like this

   id shares_float
0   1      621.76M
1   2      329.51M

in other word,

[(1, '621.76M'), (2, '329.51M')]

I want to split the shares_float so that if it is 'B', multiply 1,000,000,000 and if it is 'M', multiply 1,000,000 and if it is neither or doesn't have the trailing character just convert and assign the number.

The outcome should be a float type

   ticker_id  shares_float     float_value
0          1       621.76M    621760000.00
1          2         3.51B   3510000000.00

I am kind of a novice at pandas. Is there a way to do it in pandas? or should I convert data to list and do my manipulation in a loop and then convert it back to pandas DataFrame?

Sep 18, 2018 in Python by charlie_brown
• 7,710 points
60 views

1 answer to this question.

0 votes

You could just simply use a conversion Dictionary

In [9]:

D={'M':'*1e6', 'B':'*1e9'}
df['float_value']=df.shares_float.apply(lambda x: eval(x[:-1]+D[x[-1]]))
In [10]:

print df
   ticker_id shares_float  float_value
0          1      621.76M   621760000
1          2        3.51B  3510000000

[2 rows x 3 columns]
In [11]:

df.dtypes
Out[11]:
ticker_id         int64
shares_float     object
float_value     float64
dtype: object
answered Sep 18, 2018 by ariaholic
• 7,320 points

Related Questions In Python

0 votes
1 answer

How to extract values from a string or a sequence?

you could use the statements like name[index value ...READ MORE

answered Mar 6 in Python by Waseem
• 3,360 points
20 views
0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 3 in Python by Taj
• 840 points
16 views
0 votes
1 answer

How to get text label from SAP using pywinauto[python]

Hi. Can you please tell me what ...READ MORE

answered Jun 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
261 views
0 votes
1 answer

How to get the return value from a thread using python?

You don't need to change your existing ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,500 points

edited Dec 19, 2018 by Nymeria 6,555 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 123 views
+4 votes
6 answers
0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
26 views
0 votes
1 answer

Is there anyway to obtain the full abstract from a 'PUBmed' article using bioPython

Hey Charlie, it's certainly possible to pull ...READ MORE

answered Aug 23, 2018 in Python by ariaholic
• 7,320 points
220 views