简体   繁体   English

大熊猫,数百万和数十亿

[英]Pandas, millions and billions

I have a dataframe with this kind of data我有一个包含这种数据的数据框

1   400.00M 
2   1.94B
3   2.72B
4   -400.00M
5   13.94B

I would like to convert the data to billions so that the output would be something like this我想将数据转换为数十亿,以便输出是这样的

1   0.40 
2   1.94
3   2.72
4   -0.40
5   13.94

Note that dtype: object注意 dtype: object

Use replace with dictionary and map pd.eval使用替换字典和映射pd.eval

Sample df:

Out[1629]:
        val
1   400.00M
2     1.94B
3     2.72B
4  -400.00M
5    13.94B

d = {'M': '*0.001', 'B': ''}

s_convert = df.val.replace(d, regex=True).map(pd.eval)

Out[1633]:
1     0.40
2     1.94
3     2.72
4    -0.40
5    13.94
Name: val, dtype: float64

You can use a lambda expression if you know for a fact that you either have only millions or billions:如果您知道您只有数百万或数十亿的事实,则可以使用 lambda 表达式:

amount=["400.00M","1.94B","2.72B","-400.00M","13.94B"]
df=pd.DataFrame(amount,columns=["amount"])
df.amount.apply(lambda x: float(x[:-1]) if x[-1]=="B" else float(x[:-1])/1000)

Or a list comprehension...或列表理解...

data = {'value': ['400.00M', '1.94B', '2.72B', '-400.00M', '13.94B']}
df = pd.DataFrame(data, index = [1, 2, 3, 4, 5])
df['value'] = [float(n[:-1])/1000 if n[-1:] == 'M' else float(n[:-1]) for n in df['value']]

...though @Andy's answer is more concise. ...虽然@Andy 的回答更简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数百万/数十亿条记录中删除 MongoDB 4 中的重复项 - Removing duplicates in MongoDB 4 from millions/billions of records python-mysqldb:如何有效地从数据库中获取数百万个记录? - python-mysqldb : How to efficiently get millions/billions of records from database? 如何将列中的所有值从数千转换为数十亿? 使用 Pandas - How to convert all the values in a column, from thousands to billions? Using Pandas 通过300万个熊猫数据框行有效处理 - Efficiently process through 3 millions Pandas dataframe rows 如何将列值更新为百万美元:PANDAS - How to update a column value to millions of dollars: PANDAS 将Python pandas DataFrame中的数字格式设置为成千上万种货币 - Format numbers in a Python pandas DataFrame as currency in thousands or millions 最快的方法来比较pandas数据帧中的行和上一行以及数百万行 - Fastest way to compare row and previous row in pandas dataframe with millions of rows 是否可以用pandas过滤数亿行数据 - Is it possible to use pandas to filter hundreds of millions of rows of data Python Pandas:如何格式化具有数千,数百万等空间的浮点数 - Python Pandas : how to format a float with space between thousands, millions, etc 为什么我的 concat 方法会在我的 pandas df 中添加数百万行? - Why is my concat method adding millions of rows to my pandas df?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM