有人能用这段代码指出我的错误吗:
groupIds = allData.drop_duplicates('GroupID').set_index('Ticket')['GroupID']
allData.loc[(allData['SexGroup']!='Man') & (allData['GroupID']=='Alone'), 'GroupID'].apply(lambda x: x['Ticket'].map(groupIds).fillna('Alone'))
错误是:
Exception has occurred: TypeError
string indices must be integers
谢谢
编辑:
数据
Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked Title SexGroup Surname TicketShort FamilySize Alone IsWomanOrChild SurnameFreq GroupID
PassengerId
1 0.0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S Mr Man Braund A/5 211 2 False False 0 Alone
2 1.0 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C Mrs Woman Cumings PC 175 2 False True 1 Alone
3 1.0 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S Miss Woman Heikkinen STON/O2. 31012 1 True True 1 Alone
4 1.0 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S Mrs Woman Futrelle 1138 2 False True 1 Alone
5 0.0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S Mr Man Allen 3734 1 True False 1 Alone
编辑2:
代码应该遍历所有GroupID,其中性别是女性,当前GroupID是“单独的”。然后,如果满足这些条件,则对于该行,检查“Ticket”值是否出现在“Ticket”列的其他位置。如果是,则返回该事件的GroupID。如果没有,就让它“单独”吧
对任何想做类似事情的人来说,这对我来说都很有用:
相关问题 更多 >
编程相关推荐