python中的类型转换从float到int

时间:2016-11-20 17:35:27

标签: python pandas

我正在尝试将类型为data_df的{​​{1}}更改为float64

int

我收到以下错误。

  

对于基数为10的int()的无效文字:'17 .44'

4 个答案:

答案 0 :(得分:10)

我认为您首先需要to_numeric因为float无法投放到int

data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)

另一种解决方案首先转为float,然后转为int

data_df['grade'] = data_df['grade'].astype(float).astype(int)

样品:

data_df = pd.DataFrame({'grade':['10','20','17.44']})
print (data_df)
   grade
0     10
1     20
2  17.44

data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)
print (data_df)
   grade
0     10
1     20
2     17
data_df['grade'] = data_df['grade'].astype(float).astype(int)
print (data_df)
   grade
0     10
1     20
2     17

---

如果某些值无法转换,并且在to_numeric出错后:

  

ValueError:无法解析字符串

可以添加参数errors='coerce'以将非数字转换为NaN

如果NaN值无法转换为int,则无法看到docs

data_df = pd.DataFrame({'grade':['10','20','17.44', 'aa']})
print (data_df)
   grade
0     10
1     20
2  17.44
3     aa

data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
print (data_df)
   grade
0  10.00
1  20.00
2  17.44
3    NaN

如果想要将NaN更改为某个数字,例如0使用fillna

data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
                     .fillna(0)
                     .astype(int)
print (data_df)
   grade
0     10
1     20
2     17
3      0

小建议:

在使用errors='coerce'之前,请boolean indexing检查所有无法转换为数字的行:

print (data_df[pd.to_numeric(data_df['grade'], errors='coerce').isnull()])
  grade
3    aa

答案 1 :(得分:3)

有效的是data_df['grade'] = int(pd.to_numeric(data_df['grade'])) 方法as_type(int)会引发错误,因为它希望告诉您,没有从float到integer的精确转换是可能的,并且您将丢失信息。 我的解决方案将截断整数(即1.9将变为1),因此您可能希望在您的问题中指定是否要通过截断或舍入将float转换为整数(即​​1.9将变为2)

答案 2 :(得分:1)

我发现这对我有用,而其他早期的答案都没有为我完成这项工作:

data_df['grade'] = data_df['grade'].apply(np.int)

答案 3 :(得分:1)

发件人:

Class.forName("oracle.jdbc.driver.OracleDriver");
    String UserName = userName ;
    String Passwrod = passwrod ;
    String urlConnection = url;

    Connection conETL2=DriverManager.getConnection(urlConnection,UserName,Passwrod);  
    String sql = "MERGE INTO  CRMAS_ODS_RAW_DATA.ACCOUNT T\r\n" + 
            "      USING CRMAS_ODS_RAW_DATA.ACCOUNT_TMP S\r\n" + 
            "      ON ( T.TCCID=S.TCCID)\r\n" + 
            "    WHEN MATCHED THEN\r\n" + 
            "    UPDATE \r\n" + 
            "    SET \r\n" + 
            "      T.BATCH_ID             =S.BATCH_ID                  ,\r\n" + 
            "  T.SOURCE_SYSTEM       =S.SOURCE_SYSTEM       ,\r\n" + 
            "  T.UPDATE_DATE         =S.UPDATE_DATE         ,\r\n" + 
            "  T.MLI_LOCALID         =S.MLI_LOCALID          \r\n" + 

            "    WHERE \r\n" + 
            "    T.BATCH_ID         <>S.BATCH_ID                    OR\r\n" + 
            "  T.SOURCE_SYSTEM       <>S.SOURCE_SYSTEM       OR\r\n" + 
            "  T.TCCID               <>S.TCCID               OR\r\n" + 
            "  T.MLI_LOCALID         <>S.MLI_LOCALID         \r\n" + 

            "  WHEN NOT MATCHED THEN\r\n" + 
            "    INSERT  (T.BATCH_ID,T.SOURCE_SYSTEM,T.UPDATE_DATE,T.TCCID,T.MLI_LOCALID)      \r\n" + 
            "    VALUES  (S.BATCH_ID,S.SOURCE_SYSTEM,S.UPDATE_DATE,S.TCCID,S.MLI_LOCALID);\r\n" ;
    PreparedStatement stmt = conETL2.prepareCall(sql);

    stmt.executeQuery(); 
    conETL2.close();

需要将int更改为'int'

data_df['grade'] = data_df['grade'].astype(int)