与lambda函数相关的Python代码错误
Traceback (most recent call last):
File "C:/Users/abhisheksingh75/PycharmProjects/Practice_Machine_Learning/titanic_2.py", line 34, in <module>
else x['Fare'], axis=1)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\frame.py", line 4877, in apply
ignore_failures=ignore_failures)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\frame.py", line 4973, in _apply_standard
results[i] = func(v)
File "C:/Users/abhisheksingh75/PycharmProjects/Practice_Machine_Learning/titanic_2.py", line 33, in <lambda>
fare_means[x['Pclass']] if pd.isnull(x['Fare'])
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "C:\Users\abhisheksingh75\PycharmProjects\project_!\venv\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: (3, 'occurred at index 152')
流程以退出代码1结束 -------------------------------------------------- ---------------代码
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from sklearn import ensemble
df_titanic = pd.read_csv('C:/Users/abhisheksingh75/Downloads/train.csv')
df_titanic = pd.read_csv('C:/Users/abhisheksingh75/Downloads/test.csv')
#Drop column which are not useful from predictive prespective
df_titanic = df_titanic.drop(['Name', 'Ticket', 'Cabin'], axis=1)
#Fill null values in age column
age_mean = df_titanic['Age'].mean()
df_titanic['Age'] = df_titanic['Age'].fillna(age_mean)
#print(df_titanic.isna().any())
#fill null values in embarked column
Embarked_mode = (df_titanic['Embarked'].mode())[0][0]
df_titanic['Embarked'] = df_titanic['Embarked'].fillna(Embarked_mode)
#print(df_titanic.isna().any())
df_titanic['Gender'] = df_titanic['Sex'].map({'female':0, 'male':1}).astype(int)
df_titanic['Port'] = df_titanic['Embarked'].map({'C':1, 'S':2, 'Q':3}).astype(int)
df_titanic = df_titanic.drop(['Sex', 'Embarked'], axis=1)
cols = df_titanic.columns.tolist()
cols = [cols[1]] + cols[0:1] + cols[2:]
fare_means = df_titanic.pivot_table('Fare', index='Pclass', aggfunc='mean')
print(fare_means)
df_titanic['Fare'] = df_titanic[['Fare','Pclass']].apply(lambda x:
fare_means[x['Pclass']] if pd.isnull(x['Fare'])
else x['Fare'], axis=1)
答案 0 :(得分:0)
df_predict['Fare'] = df_predict[['Fare', 'Pclass']].apply(lambda x:
fare_means**.ix[x**['Pclass']] if pd.isnull(x['Fare'])
else x['Fare'], axis=1)
我不知道为什么我的代码之前没有工作...但是当我使用fare_mean.ix()时 function..it能够定位元素位置