I have a dataframe:
import pandas as pd
test_df = pd.DataFrame({
'_id': ['1a','2b','3c','4d'],
'column': ['und der in zu',
'Kompliziertereswort something',
'Lehrerin in zu [Buch]',
'Buch (Lehrerin) kompliziertereswort']})
and a dictionary:
{'und': 20,
'der': 10,
'in': 40,
'zu': 10,
'Kompliziertereswort': 2,
'Buch': 5,
'Lehrerin': 5}
I want to add a new column to the dataframe that represents the average of the values of the words in that row. If the word is not in that dictionary, one should just ignore it.
_id column score
1a und der in zu 20
2b Kompliziertereswort something 2
3c Lehrerin in zu [Buch] 15
4d Buch (Lehrerin) kompliziertereswort 5
I did something very silly I think, i.e.: write the dataframe as a text file, read every line; I made a list of all the keys in the dictionary and then used regex to check whether that line contains the words. It didn't work because of the brackets, probably.
I also tried to split the dataframe row, but it just splits it into separate letters:
for index, values in test_df.iterrows():
pos = 1
for x in values[1]:
print(pos, x)
pos += 1