English 中文(简体)
价格一栏标注在安达。
原标题:Price column object to int in pandas

我有一个一栏,有类似价值:3 092.44美元。 当我做什么时,dataframe.dtypes(>)将本栏作为物体,如何将该栏改为t型?

最佳回答

in regex D means not digit... so we can use pd.Series.str.replace

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

0    309244
1    309244
Name: amount, dtype: int64
问题回答

http://pandas.pydata.org/pandas-docs/stable/生成/pandas.Series.replace.html” rel=“noreferer”>Series.replace or

这就是你如何做,同时抛弃以下几条:

car_sales["Price"] = car_sales["Price"].str.replace( [$,]|.d* ,   ).astype(int)
dataframe["amount"] = dataframe["amount"].str.replace( [$,.] ,   ).astype(int)
        Make     Colour    Odometer (KM)        Doors              Price
0       Toyota   White     150043                4                 $4,000.00
1       Honda    Red       87899                 4                 $5,000.00
2       Toyota   Blue      32549                 3                 $7,000.00
3       BMW      Black     11179                 5                 $22,000.00
4       Nissan   White     213095                4                 $3,500.00
5       Toyota   Green     99213                 4                 $4,500.00
6       Honda    Blue      45698                 4                 $7,500.00
7       Honda    Blue      54738                 4                 $7,000.00
8       Toyota   White     60000                 4                 $6,250.00
9       Nissan   White     31600                 4                 $9,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

下面是简单的方法:

cars["amount"] = cars["amount"].str.replace("$" , "").str.replace("," , "").astype("float").astype("int")
  1. First you remove the dollar sign
  2. Next you remove the comma
  3. Then you convert the column to float. If you try to convert the column straight to integer, you will get the following error: Can only use .str accessor with string values!
  4. Finally you convert the column to integer

页: 1

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

如果你想将价格转化为扼杀,那么你可以采用以下方法:

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

您可以通过下列方式使:

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

如果你想把沙尔语提上“Int”一栏,则使用:

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

还将开展工作:dframe.amount.str.replace(“$””)astype(int)

这应当简单易懂,仅用“$, commas(,)和 decimals(. dots)替换成零件()并删除多余零件。

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

我认为,使用“拉姆布达”和无视美元也是更好的解决办法。

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

为了避免在将物体转换为暗中时添加ZEROs,请将物体(3 092.440美元)改为使用以下代码浮动:

Syntax:

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

<>>Example:

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

Result:

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
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)

In the above code we have to use float instead of integer so that the cent value would be remain as cents.

df[ Price ] = df[ Price ].str.replace( [$,] ,  ).astype(float)

This worked for me

car_sales = pd.read_csv("https://raw.githubusercontent.com/mrdbourke/zero-to-mastery-ml/master/data/car-sales.csv")
car_sales["Price"] = car_sales["Price"].replace("[$,.]", "", regex=True).map(lambda x: str(x)[:-2]).astype(int)
car_sales
car_sales["Price"] = car_sales["Price"].replace( [$,] ,   , regex=True).astype(float)

There are two ways to solve this problem, using or not using regex, which is a parameter that specifies whether or not you will use a regular expression, in our case ("[$,.]"). In the first case you will not use regex, which makes the code a little more verbose:

data_frame[ Price ] = data_frame[ Price ].str.replace( $ ,   ).str.replace( , ,   ).str.replace( . ,   )

data_frame["Price"] = data_frame["Price"].astype(int)

在第二种情况下,我们使用reg,但我们需要启动并使用常规表述:

data_frame["Price"] = data_frame["Price"].str.replace( [$,.] ,   , regex=True).astype(int)
汽车["Price"] = 汽车["Price"].replace( [$,] ,   , regex=True).astype(float).astype(int)

汽车





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...