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

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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]:

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
• 2,710 points
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
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 5,122 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21 in Python by Nymeria
• 3,500 points
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
+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
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

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.