【发布时间】:2015-07-29 05:29:08
【问题描述】:
我刚开始使用 Python,在玩 Kaggle Titanic 数据时遇到了一些问题。 https://www.kaggle.com/c/titanic/data
这是我在 ipython 笔记本中输入的内容(train.csv 来自上面 kaggle 链接中的泰坦尼克号数据):
import pandas as pd
df = pd.read_csv("C:/fakepath/titanic/data/train.csv")
然后我继续检查“Sex”列中是否有任何错误数据:
df['Sex'].value_counts()
返回:
male 577
female 314
dtype: int64
df['Gender'] = df['Sex'].map( {'male': 1, 'female': 0} ).astype(int)
这不会产生任何错误。要验证它是否创建了一个名为“性别”的新列,其中包含整数值:
df
返回:
# PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked Gender
0 1 0 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.2500 NaN S 1
1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38 1 0 PC 17599 71.2833 C85 C 0
2 3 1 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.9250 NaN S 0
3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.1000 C123 S 0
...成功,Gender 列追加到末尾,女性为 0,男性为 1。现在,我创建了一个新的 pandas 数据帧,它是 df 数据帧的子集。
df2 = df[ ['Survived', 'Pclass', 'Age', 'Gender', 'Embarked'] ]
df2
返回:
Survived Pclass Age Gender Embarked
0 0 3 22 1 S
1 1 1 38 0 C
2 1 3 26 0 S
3 1 1 35 0 S
4 0 3 35 1 S
5 0 3 NaN 1 Q
df2['Embarked'].value_counts()
...表明有 3 个唯一值(S、C、Q):
S 644
C 168
Q 77
dtype: int64
但是,当我尝试执行我认为与将男性/女性转换为 1/0 时相同类型的操作时,我收到错误:
df2['Embarked_int'] = df2['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2}).astype(int)
返回:
ValueError Traceback (most recent call last)
<ipython-input-29-294c08f2fc80> in <module>()
----> 1 df2['Embarked_int'] = df2['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2}).astype(int)
C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in astype(self, dtype, copy, raise_on_error)
2212
2213 mgr = self._data.astype(
-> 2214 dtype=dtype, copy=copy, raise_on_error=raise_on_error)
2215 return self._constructor(mgr).__finalize__(self)
2216
C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in astype(self, dtype, **kwargs)
2500
2501 def astype(self, dtype, **kwargs):
-> 2502 return self.apply('astype', dtype=dtype, **kwargs)
2503
2504 def convert(self, **kwargs):
C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in apply(self, f, axes, filter, do_integrity_check, **kwargs)
2455 copy=align_copy)
2456
-> 2457 applied = getattr(b, f)(**kwargs)
2458
2459 if isinstance(applied, list):
C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in astype(self, dtype, copy, raise_on_error, values)
369 def astype(self, dtype, copy=False, raise_on_error=True, values=None):
370 return self._astype(dtype, copy=copy, raise_on_error=raise_on_error,
--> 371 values=values)
372
373 def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in _astype(self, dtype, copy, raise_on_error, values, klass)
399 if values is None:
400 # _astype_nansafe works fine with 1-d only
--> 401 values = com._astype_nansafe(self.values.ravel(), dtype, copy=True)
402 values = values.reshape(self.values.shape)
403 newb = make_block(values,
C:\Anaconda\lib\site-packages\pandas\core\common.pyc in _astype_nansafe(arr, dtype, copy)
2616
2617 if np.isnan(arr).any():
-> 2618 raise ValueError('Cannot convert NA to integer')
2619 elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer):
2620 # work around NumPy brokenness, #1987
ValueError: Cannot convert NA to integer
知道为什么我在第二次使用 map 函数时遇到此错误,但不是第一次吗?每个 value_counts() 的 Embarked 列中没有 NAN 值。我猜这是一个菜鸟问题:)
【问题讨论】:
-
我认为你不需要那些
astype(int)s,顺便说一句。 -
有些东西没有加起来你的性别有 891 行
value_counts但你的出发有 889 行,这意味着你必须有NaN值,docs 声明NaN值如果您尝试df['Embarked'].value_counts(dropna=False),您可以确认这一点,这意味着您需要通过在调用 map 之前先填充它们来处理 NaN -
报错信息的最后一行:ValueError: Cannot convert NA to integer。您可能必须从数据框中删除 NA。
-
关于多余的 .astype(int),我觉得它在那里很奇怪。从 Kaggle 的教程中复制的。感谢您的澄清! @阿米
标签: python pandas ipython-notebook