数据框套用(地图)函数TypeError

时间:2019-03-24 17:39:55

标签: python pandas dataframe lambda apply

有人可以用这段代码指出我的方式的错误吗?

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为“ Alone”的GroupID。然后,如果满足这些条件,则对于该行,检查“票证”值是否出现在“票证”列的其他位置。如果是,则返回该事件的GroupID。如果不是,请将其保留为“单独”

1 个答案:

答案 0 :(得分:0)

对于任何想做类似事情的人来说,这对我有用:

allData.loc[(allData['SexGroup']!='Man') & (allData['GroupID']=='Alone'), 'GroupID'] = allData['Ticket'].map(groupIds).fillna('Alone')