【问题标题】:Converting data type of a column in a csv file转换csv文件中列的数据类型
【发布时间】:2017-10-28 01:09:17
【问题描述】:

我试图使用 Numpy 和 Pandas 库在 Pycharm 中修改 Python 中列的数据类型,但出现以下错误。

dataset.fillna(1e6).astype(int)

D:\Softwares\Python3.6.1\python.exe D:/PythonPractice/DataPreprocessing/DataPreprocessing_1.py
Traceback (most recent call last):
   Country   Age   Salary Purchased
  File "D:/PythonPractice/DataPreprocessing/DataPreprocessing_1.py", line 6, in <module>
    dataset.fillna(1e6).astype(int)
0   France  44.0  72000.0        No
1    Spain  27.0  48000.0       Yes
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\util\_decorators.py", line 91, in wrapper
2  Germany  30.0  54000.0        No
3    Spain  38.0  61000.0        No
    return func(*args, **kwargs)
4  Germany  40.0      NaN       Yes
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\generic.py", line 3299, in astype
    **kwargs)
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\internals.py", line 3224, in astype
5   France  35.0  58000.0       Yes
    return self.apply('astype', dtype=dtype, **kwargs)
6    Spain   NaN  52000.0        No
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\internals.py", line 3091, in apply
7   France  48.0  79000.0       Yes
    applied = getattr(b, f)(**kwargs)
8  Germany  50.0  83000.0        No
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\internals.py", line 471, in astype
9   France  37.0  67000.0       Yes
    **kwargs)
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\internals.py", line 521, in _astype
    values = astype_nansafe(values.ravel(), dtype, copy=True)
  File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\dtypes\cast.py", line 625, in astype_nansafe
    return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
  File "pandas\_libs\lib.pyx", line 917, in pandas._libs.lib.astype_intsafe (pandas\_libs\lib.c:16260)
  File "pandas\_libs\src\util.pxd", line 93, in util.set_value_at_unsafe (pandas\_libs\lib.c:73093)
ValueError: invalid literal for int() with base 10: 'France'

【问题讨论】:

  • ValueError: int() 基数为 10 的无效文字:'France'
  • 这意味着您期望整数类型但得到字符串。 法国字在铸造方法中传递

标签: python csv pandas numpy pycharm


【解决方案1】:

您的错误消息 - ValueError: invalid literal for int() with base 10: 'France' - 表明您正在使用 Country 列,其内容是字符串,不能更改为整数。试着调整你的范围。

【讨论】:

  • dataset.iloc[:, 1:3].fillna(1e6).astype(int) print(dataset.dtypes) 仍然将 float64 作为包含南的列的数据类型
  • 您必须将系列实际设置为等于新的 dtype。 dataset.iloc[:, 1:3] = dataset.iloc[:, 1:3].astype(int).
  • Traceback (most recent call last): File "D:/PythonPractice/DataPreprocessing/DataPreprocessing_1.py", line 7, in &lt;module&gt; dataset.iloc[:, 1:3] = dataset.iloc[:, 1:3].astype(int) File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\util\_decorators.py", line 91, in wrapper return func(*args, **kwargs) ....... File "D:\Softwares\Python3.6.1\lib\site-packages\pandas\core\dtypes\cast.py", line 620, in astype_nansafe raise ValueError('Cannot convert non-finite values (NA or inf) to ' ValueError: Cannot convert non-finite values (NA or inf) to integer
  • 是的!谢谢!!我能够做到:)
【解决方案2】:

您不能将“France”转换为整数,您应该:

    dataset['Country'] = dataset['Country'].map({'France': 0, 'Spain': 1, 'Germany': 2})]

然后:

    dataset['Country'].astype(int)

如果还有这样的错误:

ValueError: Cannot convert non-finite values (NA or inf) to integer

这是因为dataset['Country']中有一些NaN

通过fillna()drop()等处理这些NaN,你会解决的。

【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多