[英]Remove numbers from a string with a letter in front
combined['Cabin'] = combined['Cabin'].map(lambda c : c[0])
我正在關注一個教程,除了這行拋出錯誤TypeError: 'float' object has no attribute '__getitem__'
有任何解決這個問題的方法嗎?
我的專欄數據看起來像Column
謝謝!
def process_cabin():
global combined
# replacing missing cabins with U (for Unknown)
combined.Cabin.fillna('U',inplace=True)
# mapping each Cabin value with the cabin letter
combined['Cabin'] = combined['Cabin'].map(lambda c : c[0])
# dummy encoding ...
cabin_dummies = pd.get_dummies(combined['Cabin'],prefix='Cabin')
combined = pd.concat([combined,cabin_dummies],axis=1)
combined.drop('Cabin',axis=1,inplace=True)
status('cabin')
`
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-152-70714b711c6d> in <module>()
----> 1 process_cabin()
<ipython-input-151-d9bb11cabd2c> in process_cabin()
7
8 # mapping each Cabin value with the cabin letter
----> 9 combined['Cabin'] = combined['Cabin'].map(lambda c : c[0])
10
11
C:\Users\Data.Steve-PC\Python\Anaconda\lib\site-packages\pandas\core\series.pyc in map(self, arg, na_action)
2014 index=self.index).__finalize__(self)
2015 else:
-> 2016 mapped = map_f(values, arg)
2017 return self._constructor(mapped,
2018 index=self.index).__finalize__(self)
pandas\src\inference.pyx in pandas.lib.map_infer (pandas\lib.c:58435)()
<ipython-input-151-d9bb11cabd2c> in <lambda>(c)
7
8 # mapping each Cabin value with the cabin letter
----> 9 combined['Cabin'] = combined['Cabin'].map(lambda c : c[0])
10
11
TypeError: 'float' object has no attribute '__getitem__'
我懷疑問題是你的X
不是你想象的那樣。 如果它是浮點數,例如,123.45:
>>> 123.45['Y']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object has no attribute '__getitem__'
我不確定你關心的是什么。 這是你想要做的嗎?
import pandas as pd
X = pd.DataFrame({'Y': ["NAN", "C85", "NAN", "C123", "NAN"]})
print X
Y
0 NAN
1 C85
2 NAN
3 C123
4 NAN
lambda_f = lambda c: c[0]
X['Y'] = map(lambda_f, X['Y'])
print X
Y
0 N
1 C
2 N
3 C
4 N
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.