df中A列的数据存在在列表B中就把A 的该行提取出来组成新的df
你可以使用isin()函数来检查A列的值是否存在于列表B中,并使用布尔索引来提取新的DataFrame。
下面是一个示例代码:
import pandas as pd
# 创建示例DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e']})
# 列表B
B = [2, 4, 6]
# 使用isin()函数检查A列的值是否存在于列表B中
mask = df['A'].isin(B)
# 使用布尔索引提取新的DataFrame
new_df = df[mask]
print(new_df)
输出结果为:
A B
1 2 b
3 4 d
新的DataFrame new_df 包含了A列的值存在于列表B中的行。
原文地址: https://www.cveoy.top/t/topic/h5FQ 著作权归作者所有。请勿转载和采集!