0

I have the following df:

df = pd.DataFrame({'all_tokens': ['BTC', 'WETH', 'SOL', 'sBTC', 'sWETH', 'sSOL', 'AVAX', 'LTC', 'ETC'],
                   'amount': [10, 20, 30, 40, 50, 60, 70, 80, 90]})

Output:

  all_tokens  amount
0        BTC      10
1       WETH      20
2        SOL      30
3       sBTC      40
4      sWETH      50
5       sSOL      60
6       AVAX      70
7        LTC      80
8        ETC      90

And the following lists:

base_tokens = ['BTC', 'WETH', 'SOL']
other_tokens = ['sBTC', 'sWETH', 'sSOL']

I want to create a new_df only with values in the 'all_tokens' column in the df that are not in either list.

Expected output:

  new_tokens
0       AVAX
1        LTC
2        ETC

I have tried:

new_df = pd.DataFrame({'new_tokens': df['all_tokens'].loc[(~df['all_tokens'].isin(base_tokens)) & (~df['all_tokens'].isin(other_tokens))]})

No success. My result is a new_df with all tokens from both lists.

Luiz Scheuer
  • 305
  • 1
  • 10

1 Answers1

1

Try this:

# Combine two list and convert them to set 
set_all = set(base_tokens + other_tokens)

res = df.loc[~df['all_tokens'].isin(set_all), 'all_tokens'].to_frame().reset_index(drop=True)

print(res)

Output:

  all_tokens
0       AVAX
1        LTC
2        ETC
I'mahdi
  • 23,382
  • 5
  • 22
  • 30