Issue
Hi sorry if this is an obvious one, looked around online and I can't seem to find what I'm doing wrong.
I am trying to compare the contents of two lists, in two separate csv files (file A and file B). Both csv files are of x rows, but only 1 column each. File A consists of rows with sentences in each, file B consists of single words. My goal is to search the rows in file B, and if any of these rows appear in file A, append the relevant rows from file A to a separate, empty list to be exported. The code I am running is as follows:
import pandas as pd
#Importing csv files
##File A is 2 rows of sentences in 1 column, e.g. "This list should be picked up for the word DISHWASHER" and "This sentence should not appear in list_AB"
file_A = pd.read_csv(r"C:\Users\User\Desktop\File\file_A.csv")
##File B is 2 rows of singular words that should appear in file A e.g. "DISHWASHER", "QWERTYXYZ123"
file_B = pd.read_csv(r"C:\Users\User\Desktop\File\file_B.csv", converters={i: str for i in range(10)})
#Convert csv files to lists
file_A2 = file_A.values.tolist()
file_B2 = file_B.values.tolist()
#Empty list
list_AB = []
#for loop supposed to filter file_A based on file_B
for x in file_A2:
words = x[0].split(" ")
#print(words)
for y in file_B2:
#print(y)
if y in words:
list_AB.append(x)
print(list_AB)
The problem is that print(list_AB) only returns an empty list ([]), not a filtered version of file_A. The reason I want to do it this way is because the actual csv files I want to read consist of 21600 (file A) and 50400 (file B) rows. Apologies in advance if this is a really basic question.
Edit: Added images of csv file examples, couldn't see how to upload files.
Solution
The problem is in the if-statement y in words
.
Here y
is a list. You're searching for a list inside a list of strings (not a list of lists).
Using y[0] in words
solve your problem.
Answered By - Filippe O. Gonçalves
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.