[英]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.