我有下面的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]})输出:
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和下列清单:
base_tokens = ['BTC', 'WETH', 'SOL']
other_tokens = ['sBTC', 'sWETH', 'sSOL']我只想创建一个new_df,其值位于df中的'all_tokens'列中,而也不在列表中。
预期产出:
new_tokens
0 AVAX
1 LTC
2 ETC我试过:
new_df = pd.DataFrame({'new_tokens': df['all_tokens'].loc[(~df['all_tokens'].isin(base_tokens)) & (~df['all_tokens'].isin(other_tokens))]})没有成功。我的结果是一个包含两个列表中所有标记的new_df。
发布于 2022-10-17 17:39:51
试试这个:
# 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)输出:
all_tokens
0 AVAX
1 LTC
2 ETChttps://stackoverflow.com/questions/74101091
复制相似问题