简体   繁体   中英

Usage of loc for lists in a pandas column

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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM