Home > other >  Usage of loc for lists in a pandas column
Usage of loc for lists in a pandas column

Time:06-14

Considering the following pandas dataframe with a single row:

data = [[['A'], '0', ['B']]]
dffsm = pd.DataFrame(data, columns=['State', 'Trigger', 'NextState'])

The following line would filter the row properly, as the datatype is string:

dffsm.loc[dffsm['Trigger'] == '0']

However, a similar line would not filter the row based on a particular list:

dffsm.loc[dffsm['State'] == ['A']]

Interestingly, dffsm.at[0,'State'] yields ['A'], but .loc would not filter the same value.

What do I have to do if I want to filter a pandas column containing lists based on a list?

CodePudding user response:

you use isin and then do a double parenthesis around the value to search for

dffsm.loc[dffsm['State'].isin([['A']])]
    State   Trigger     NextState
0   [A]         0       [B]
  • Related