如何在Python中将以字符表示的数字转换为数字 [英] How to convert numbers represented as characters for short into numeric in Python

查看:1824
本文介绍了如何在Python中将以字符表示的数字转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框中有一列,其值类似"3.456B",实际上代表34.56亿(和百万的类似表示法).如何将该字符串形式转换为正确的数字表示形式?

I have a column in my data frame which has values like '3.456B' which actually stands for 3.456 Billion (and similar notation for Million). How to convert this string form to correct numeric representation?

这显示了数据框:

import pandas as pd
data_csv = pd.read_csv('https://biz.yahoo.com/p/csv/422conameu.csv')
data_csv

这是一个示例值:

data_csv['Market Cap'][0]
type(data_csv['Market Cap'][0])

我尝试过:

data_csv.loc[data_csv['Market Cap'].str.contains('B'), 'Market Cap'] = data_csv['Market Cap'].str.replace('B', '').astype(float).fillna(0.0)
data_csv

但是不幸的是,最后还有带有"M"的值,表示百万.它返回错误,如下所示:

But unfortunately there are also values with 'M' at the end which denotes Millions. It returns error as follows:

ValueError: invalid literal for float(): 6.46M

如何用此列中的适当值替换B和M?有更好的方法吗?

How can I replace both B and M with appropriate values in this column? Is there a better way to do it?

推荐答案

假定所有条目的末尾都有一个字母,您可以执行以下操作:

Assuming all entries have a letter at the end, you can do this:

d = {'K': 1000, 'M': 1000000, 'B': 1000000000}
df.loc[:, 'Market Cap'] = pd.to_numeric(df['Market Cap'].str[:-1]) * \
    df['Market Cap'].str[-1].replace(d)

这会将除最后一个字符以外的所有内容转换为数字值,然后将其乘以等于最后一个字符中字母的数字.

This converts everything but the last character into a numeric value, then multiplies it by the number equivalent to the letter in the last character.

这篇关于如何在Python中将以字符表示的数字转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆