Reputation: 458
I have two columns that I am trying to iterate through and compare the data of the Pin column to each separate number in the Adj. column and throw a flag if they match. Row 0 Pin only has to compare with each number in row 0 Adj.. I have created a dictionary in which each number corresponds to a color, with several numbers being the same color. If the "color" in the Pin column matches the "color" in the Adj. column I want make a basic print statement. The issue that I am having is using the dictionary to compare the values.
Table
Sample Spreadsheets Being Used
import pandas as pd
excel_file = r'C:\Users\449287\Documents\Sample.adjacent.xlsx'
excel_file1 = r'C:\Users\449287\Documents\Sample.Pin.Out.xlsx'
df = pd.read_excel(excel_file, sheetname='Sheet1')
df1 = pd.read_excel(excel_file1, sheetname='Sheet1')
df1d = {1: "R",2: "O",3: "Y", 4: "GR", 5: "L", 6: "R", 7: "B", 8: "L", 9: "GR", 10: "O"}
"""
df1.set_index('Pin')['Color'].to_dict()
"""
df['Adj.'] = df['Adj.'].str.replace(',', ' ')
Upvotes: 0
Views: 295
Reputation: 26
At first, you will need a code for comparing values from 'Adj.' column with values for same color. You can put it in function:
def check_color(dictionary, value, adj_values):
for x in adj_values.split():
if int(x) in [k for k, v in dictionary.items() if v == dictionary[value]]:
print('Your statement')
return
Now, you have to just iterate over rows and for each row, call function above for parameters (dictionary that you have already declared, value from 'Pin' column and value from 'Adj.' column):
for index, row in df.iterrows():
check_color(df1d, row['Pin'], row['Adj.'])
Hope that this helps. If you will have any questions, feel free to ask.
Upvotes: 1