从前面带字母的字符串中删除数字

时间:2016-09-07 04:10:27

标签: python

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__'

2 个答案:

答案 0 :(得分:0)

我怀疑问题是你的X不是你认为的那样。如果它是浮点数,例如,123.45:

>>> 123.45['Y']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object has no attribute '__getitem__'

答案 1 :(得分:0)

我不确定你的顾虑是什么。这是你想要做的吗?

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