[英]How to convert the column in US million dollars in pandas?
I have a column called collection as follow 我有一个名为collection的列如下
collection : $5,345,677, 46836214, $533,316,061, " ", 29200000 收集:$ 5,345,677,46836214,$ 533,316,061,“”,29200000
Column values have both in US dollar and without dollars. 列值既有美元也有美元。 Also, it has NAN.
此外,它有NAN。 I want to change into US Dollar in million
我想换成百万美元
I used to convert as follow but not successful 我以前转换如下,但没有成功
df['Boxoffice in US$ (mil)'] = (df2['collection'].astype(float)/1000000).round(2).astype(str)
Getting this error: could not convert string to float: '$5,345,677' 得到此错误:无法将字符串转换为浮点数:'$ 5,345,677'
Please advise 请指教
# remove the '$' and ',' from the strings so it can be converted to numerics
# -> notice: the series is converted to strings to handle numerics (eg. 29200000)
collection_tmp = df2['collection'].astype(str).str.replace('[$,]', '')
# convert to numerics (floats) and then to millions
# -> errors='coerce' sets NaN for invalid values
millions = pd.to_numeric(collection_tmp, errors='coerce')/1e6
# create 'Boxoffice in US$ (mil)'
df['Boxoffice in US$ (mil)'] = millions.round(2).astype('str')
You can refer to the following step: 您可以参考以下步骤:
1.Fill NAN or blank value (white space). 1.填写NAN或空白值(空白区域)。 You said it has Nan, but i saw " ".
你说它有南,但我看到了“”。
[in ]: df['collection']
[out]: collection
0 $5,345,677
1 46836214
2 $533,316,061
3
4 29200000
[in ]: # if you have Nan, just use method `fillna` instead
# like df['collection'].fillna('0')
[in ]: df['collection'].replace(r'^\s*$', '0', regex=True)
[out]: collection
0 $5,345,677
1 46836214
2 $533,316,061
3 0
4 29200000
2.Then covert number to 'US Dollar in million'. 然后将数字转换为“百万美元”。
[in ]: df['collection'].apply(lambda x: ''.join(('$', format(int(x), ','))) if not '$' in x else x)
[out]: collection
0 $5,345,677
1 $46,836,214
2 $533,316,061
3 $0
4 $29,200,000
I do hope this can help! 我希望这可以帮到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.