简体   繁体   English

在python中转换int

[英]converting int in python

Data frame= reviews 数据框= reviews

I get the following errror when I try to convert rating column to integer 当我尝试将评级列转换为整数时出现以下错误

''Cannot convert non-finite values (NA or inf) to integer'' ``无法将非有限值(NA或inf)转换为整数''

how can I fix it? 我该如何解决?

reviews.replace([np.inf, -np.inf], np.nan)
reviews.dropna() 

reviews['Rating'].astype('int')

The simplest way would be to first replace infs to NaN and then use dropna : 最简单的方法是先将dropna replace为NaN,然后​​使用dropna

Example DataFrame: 示例数据框:

>>> df = pd.DataFrame({'col1':[1, 2, 3, 4, 5, np.inf, -np.inf], 'col2':[6, 7, 8, 9, 10, np.inf, -np.inf]})

>>> df
       col1       col2
0  1.000000   6.000000
1  2.000000   7.000000
2  3.000000   8.000000
3  4.000000   9.000000
4  5.000000  10.000000
5       inf        inf
6      -inf       -inf

Solution 1: 解决方案1:

Create a df_new that way you will not loose the real dataframe and desired dataFrame will ne df_new separately.. 以这种方式创建df_new ,您将不会丢失实际的数据帧,而所需的dataFrame会分别与df_new分离。

>>> df_new = df.replace([np.inf, -np.inf], np.nan).dropna(subset=["col1", "col2"], how="all").astype(int)
>>> df_new
   col1  col2
0     1     6
1     2     7
2     3     8
3     4     9
4     5    10

Solution 2: 解决方案2:

using isin and ~ : 使用isin~

>>> ff = df.isin([np.inf, -np.inf, np.nan]).all(axis='columns')
>>> df[~ff].astype(int)
   col1  col2
0     1     6
1     2     7
2     3     8
3     4     9
4     5    10

OR Directly into original Dataframe, Use pd.DataFrame.isin and check for rows that have any with pd.DataFrame.any . 或直接使用原始数据框,使用pd.DataFrame.isin并检查带有pd.DataFrame.any行。 Finally, use the boolean array to slice the dataframe. 最后,使用布尔数组对数据帧进行切片。

>>> df = df[~df.isin([np.nan, np.inf, -np.inf]).any(1)].astype(int)
>>> df
   col1  col2
0     1     6
1     2     7
2     3     8
3     4     9
4     5    10

above taken from here courtesy to the @piRSquared 从上方拍摄这里的礼貌的@piRSquared

Solution 3: 解决方案3:

You have liberty to use dataFrame.mask + numpy.isinf and the using dronna() : 您可以自由使用dataFrame.mask + numpy.isinf和使用dronna()

>>> df = df.mask(np.isinf(df)).dropna().astype(int)
>>> df
   col1  col2
0     1     6
1     2     7
2     3     8
3     4     9
4     5    10

Both .replace() and .dropna() do not perform their actions in place, eg modify the existing dataframe unless you specify them to. .replace().dropna()都不会执行它们的操作,例如,除非您指定它们,否则请修改现有数据.dropna() However if you do specify to perform them in place your code would work: 但是,如果您确实指定执行它们,您的代码将起作用:

reviews.replace([np.inf, -np.inf], np.nan, inplace=True)
reviews.dropna(inplace=True) 

reviews['Rating'].astype('int')

Or: 要么:

reviews = reviews.replace([np.inf, -np.inf], np.nan)
reviews = reviews.dropna() 

reviews['Rating'].astype('int')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM