I'm trying to compare a list
and a dataframe
. If an item
in the list
equals a value from the first column in the dataframe's row
, I would like to print out that list's item
with the dataframe's second column
value after it.
If no items
in the list
match any items
in the dataframe's
second column, I would like to just print out the list's item
. I thought a good way to go about this is to iterate through the whole list
and dataframe
, and if we get to the last row of the dataframe
and non of the items match, print out just the list's item
instead of the list's item
plus the dataframe's second column
.
What I need help with is determining the syntax needed to find the last row in the dataframe. Please see my code below.
The dataframe I'm using is 1003 rows X 2 columns
. The row labels are numbers 0-1002
. The column labels are col1
and col2
#compare items from List against items from dataframe to find matches
for item in List:
for idx, row in df.iterrows():
if item in row['col1']:
print str(count) + " " + str(item) + " " + str(row['col2'])
count=count+1
#if it's the last row in dataframe:
if item not in row['col1']:
print str(count) + " " + str(item)