pandas 如何解析 DataFrame 列中的所有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37351183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse all the values in a column of a DataFrame?
提问by Rakesh Adhikesavan
DataFrame df has a column called amount
DataFrame df 有一列名为 amount
import pandas as pd
df = pd.DataFrame([',000,000.00',',000.00', '0.5', '.5'], columns = ['Amount'])
df:
df:
ID | Amount
0 | ,000,000.00
1 | ,000.00
2 | 0.5
3 | .5
I want to parse all the values in column amount and extract the amount as a number and ignore the decimal points. End result is DataFrame that looks like this:
我想解析列数量中的所有值并将数量提取为数字并忽略小数点。最终结果是如下所示的 DataFrame:
ID | Amount
0 | 3000000
1 | 3000
2 | 200
3 | 5
How do I do this?
我该怎么做呢?
回答by jezrael
You can use str.replace
with double casting by astype
:
您可以通过以下方式str.replace
使用双重铸造astype
:
df['Amount'] = (df.Amount.str.replace(r'[$,]', '').astype(float).astype(int))
print (df)
Amount
0 3000000
1 3000
2 200
3 5
回答by Vedang Mehta
Code -
代码 -
import pandas as pd
def format_amount(x):
x = x[1:].split('.')[0]
return int(''.join(x.split(',')))
df = pd.DataFrame([',000,000.00',',000.00', '0.5', '.5'], columns =
['Amount'])
df['Amount'] = df['Amount'].apply(format_amount)
print(df)
Output -
输出 -
Amount
0 3000000
1 3000
2 200
3 5
回答by Daniel Velkov
You need to use the map function on the column and reassign to the same column:
您需要在列上使用 map 函数并重新分配给同一列:
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
df.Amount = df.Amount.map(lambda s: int(locale.atof(s[1:])))
PS: This uses the code from How do I use Python to convert a string to a number if it has commas in it as thousands separators?to convert a string representing a number with thousands separator to an int
PS:这使用了How do I use Python to convert a string to a number 中的代码,如果它有逗号作为千位分隔符?将表示带有千位分隔符的数字的字符串转换为 int