pandas 中的价格列对象为 int

Price column object to int in pandas

我有一个名为 amount 的列,其中包含如下所示的值:3,092.44 美元,当我这样做时 dataframe.dtypes() returns 将此列作为对象如何将此列转换为 int 类型?

您可以通过以下方式将其设置为 Int:

df['amount'] = df['amount'].astype(np.int)

如果你想告诉 Python 首先将列读取为 Int,请使用:

#assuming you're reading from a file
pd.read_csv(file_name, dtype={'amount':np.int32})

假设您的列名称是 amount,这是您应该做的:

dataframe['amount'] = dataframe.amount.str.replace('$|\.|\,', '').astype(int)

您可以使用 Series.replace or Series.str.replace with Series.astype:

dataframe = pd.DataFrame(data={'amount':[',092.44', ',092.44']})
print (dataframe)
      amount
0  ,092.44
1  ,092.44

dataframe['amount'] = dataframe['amount'].replace('[$\,\.]', '', regex=True).astype(int)

print (dataframe)
   amount
0  309244
1  309244

dataframe['amount'] = dataframe['amount'].astype(int)

print (dataframe)
   amount
0  309244
1  309244

在正则表达式中 \D 表示不是数字...所以我们可以使用 pd.Series.str.replace

dataframe.amount.replace('\D', '', regex=True).astype(int)

0    309244
1    309244
Name: amount, dtype: int64

这就是您在丢弃美分时的做法:

car_sales["Price"] = car_sales["Price"].str.replace('[$\,]|\.\d*', '').astype(int)

这也适用:dframe.amount.str.replace("$","").astype(int)

dataframe["amount"] = dataframe["amount"].str.replace('[$\,\.]', '').astype(int)
        Make     Colour    Odometer (KM)        Doors              Price
0       Toyota   White     150043                4                 ,000.00
1       Honda    Red       87899                 4                 ,000.00
2       Toyota   Blue      32549                 3                 ,000.00
3       BMW      Black     11179                 5                 ,000.00
4       Nissan   White     213095                4                 ,500.00
5       Toyota   Green     99213                 4                 ,500.00
6       Honda    Blue      45698                 4                 ,500.00
7       Honda    Blue      54738                 4                 ,000.00
8       Toyota   White     60000                 4                 ,250.00
9       Nissan   White     31600                 4                 ,700.00
car_sales["Price"].dtype
output-dtype('O')

car_sales["Price"]=car_sales["Price"].str.replace('[$\,\.]', '').astype(int)
car_sales["Price"]

输出:

0     400000
1     500000
2     700000
3    2200000
4     350000
5     450000
6     750000
7     700000
8     625000
9     970000
Name: Price, dtype: int32

这应该很简单,只需将 $、逗号 (,) 和小数点 (. 点) 替换为任何内容 ('') 并删除多余的零,它会起作用。

your_column_name = your_column_name.str.replace('[$\,]|\.\d*', '').astype(int)

我认为使用 lambda 并忽略 $ 也是更好的解决方案

dollarizer = lambda x: float(x[1:-1])
dataframe.amount = dataframe.amount.apply(dollarizer)

在将对象转换为 int 时避免额外的零。您应该使用以下代码将对象 ($3,092.440) 转换为浮点数:

语法:

your_dataframe["your_column_name"] = your_dataframe["your_column_name"].str.replace('[$\,]', '').astype(float)

示例:

car_sales["Price"] = car_sales["Price"].replace('[$\,]', '').astype(float)

结果:

4000.0
dataframe["amount"] = dataframe["amount"].str.replace('[$,.]|..$','',regex=True).astype(int)

in str.replace(...)

[$,.] mean find $ , .
| mean or

..$ mean find any last 2 character

so '[$,.]|..$' mean find $ , . or any last 2 character

如果您想将价格转换为字符串,可以使用以下方法:

car_sales["Price"] = car_sales["Price"].replace('[$\,]', '').astype(str)

car_sales["Price"]

0     400000
1     500000
2     700000
3    2200000
4     350000
5     450000
6     750000
7     700000
8     625000
9     970000
Name: Price, dtype: object

这里有一个简单的方法:

cars["amount"] = cars["amount"].str.replace("$" , "").str.replace("," , "").astype("float").astype("int")
  1. 首先删除美元符号
  2. 接下来删除逗号
  3. 然后将该列转换为浮点数。如果您尝试将列直接转换为整数,您将收到以下错误:Can only use .str accessor with string values!
  4. 最后将该列转换为整数
export_car_sales["Price"] = export_car_sales["Price"].replace('[$\,\.]', '', regex=True).astype(int)

试试这个:

car_sales["Price"] = car_sales["Price"].str.replace('[$\,]|\.\d*', '').astype(int)

但是您必须将其除以 100 以删除将要创建的额外零,因此您将必须 运行 此附加指令:

car_sales["Price"]=car_sales["Price"].apply(lambda x: x/100)