本文介绍了在数据框中将字符转换为数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个称为"XLK"的df:
I have a df called 'XLK':
Market Cap PE
AAN 3.25B 23.6
AAPL 819.30B 18.44
ACFN 6.18M 2.1
ACIW 2.63B 103.15
我只希望市值大于1亿,所以预期输出为:
I just want the market cap for values > 100 million, so expected output is:
Market Cap PE
AAN 3.25B 23.6
AAPL 819.30B 18.44
ACIW 2.63B 103.15
我尝试将字母转换为适当的0失败了:
I've tried converting the letters to the appropriate 0's with no success:
XLK['Market Cap'].replace('M','000000')
XLK.drop[XLK_quote['Market Cap'] < '100M'].index
推荐答案
将replace
与regex=True
结合使用,并使用模拟科学计数法的替换字符串.然后使用astype(float)
或pd.to_numeric
.
Use replace
with regex=True
and use replacement strings that emulate scientific notation. Then use astype(float)
or pd.to_numeric
.
df[df.Market_Cap.replace(dict(B='E9', M='E6'), regex=True).astype(float) >= 100E6]
Market_Cap PE
AAN 3.25B 23.60
AAPL 819.30B 18.44
ACIW 2.63B 103.15
等价
Equivalently
dct = dict(B='E9', M='E6')
num = pd.to_numeric(df.Market_Cap.replace(dct, regex=True), 'coerce')
df[num >= 100E6]
这篇关于在数据框中将字符转换为数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!