How to see if a string in a pandas column appears in a list

前端 未结 1 610
抹茶落季
抹茶落季 2021-01-29 08:42

I have a Pandas DataFrame, called Matches with a column called MatchIDs. This contains a selection of Match IDs as below:

Man CityTotte         


        
1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 09:24

    Use isin for check values of column MatchIDs in second df called Rivals, last cast boolean mask to int by astype:

    Matches['new'] = Matches['MatchIDs'].isin(Rivals['MatchIDs']).astype(int)
    

    Also if values are in list:

    Matches['new'] = Matches['MatchIDs'].isin(L).astype(int)
    

    0 讨论(0)
提交回复
热议问题