我有一个包含 2 列感兴趣的数据框。两者都充满了弦。我还有一个映射键值对的字典,它们也是字符串。我使用字典的键按第一列过滤数据帧,仅查找字典中的那些键。
最终目标是查找数据帧的第一列与字典中的键匹配,然后确认第二列的值与字典中的值匹配。
感兴趣的键上的过滤数据框按预期工作,因此我留下了一个包含两列的数据框,其中仅包含字典中存在的列键。过滤后的数据帧可以是从几行到数千行的任意位置,但字典的长度是静态的。
最终输出应该是一个数据帧,其内容显示已过滤数据帧的行,其中第二列的值与字典的值不匹配。
pairs = {'red': 'apple', 'blue': 'blueberry', 'yellow':'banana'}
filtered_data = {'Color':['red', 'blue'], 'Fruit':['appl','blueberry']}
filtered_df = pd.DataFrame(filtered_data)
#so the filtered_df would resemble
Color Fruit
red appl
blue blueberry
for row in filtered_df.iterrows():
for k,v in pairs.items():
#Here's where I'd like to check the value of column 1, find it in the dict then if the
#values dont match between col 2 in the df and the dict, append the mismatched row to a
#new df.
if …Run Code Online (Sandbox Code Playgroud)