根据DataFrame A列的值筛选DataFrame B中的数据
import pandas as pd # 创建示例 DataFrame A dataA = {'ID': [1, 2, 3, 4], 'Category': ['A', 'B', 'A', 'C']} dfA = pd.DataFrame(dataA) # 创建示例 DataFrame B dataB = {'ID': [1, 2, 3, 4], 'Value': [100, 200, 300, 400]} dfB = pd.DataFrame(dataB) # 根据 DataFrame A 中 'Category' 列的值筛选 DataFrame B category_to_select = 'A' filtered_dfB = dfB[dfB['ID'].isin(dfA[dfA['Category'] == category_to_select]['ID'])] # 打印筛选后的 DataFrame B print(filtered_dfB)
本文来自博客园,作者:OTAKU_nicole,转载请注明原文链接:https://www.cnblogs.com/nicole-zhang/p/17613992.html