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)
标签:Category,dfA,DataFrame,pd,筛选,数据,ID,dfB From: https://www.cnblogs.com/nicole-zhang/p/17613992.html