I've run into an issue when trying to extract values (in order to count them) from a .csv file while using a for loop to go through a list to try and find the correct values.
The .csv file is structured as follows:
word,pleasantness,activation,imagery
a,2.0000,1.3846,1.0
abandon,1.0000,2.3750,2.4
abandoned,1.1429,2.1000,3.0
abandonment,1.0000,2.0000,1.4
etc...
The first column contains a list of ~9000 words and the 3 others columns contain values that are of linguistic relevance to that specific word.
I used pandas to create a dataframe:
df = pd.read_csv("dictionary.csv", sep=',')
I've also got a text files which I've turned into a list:
read_file = open(textfile)
data = read_file.read().split()
Now, my goal is to have the program go through each word in the list and every time one of those words is encountered in the first column of the .csv file it will add its values to the existing variables. And so on until it's reached the end of the list.
count = 0
pleasantness = 0
activation = 0
imagery = 0
for w in data:
count = count + 1
if w in df.word:
pleasantness = pleasantness + df.pleasantness
activation = activation + df.activation
imagery = imagery + df.imagery
print(count, pleasantness, activation, imagery)
This is the best I've been able to come up with and it clearly doesn't work; by the end of it the variables are all still 0.
Does anyone have a clue as to how to do this? It naturally doesn't have to be done using something similar to this approach; I merely care about getting the results.