Issue
I'm trying to create a sentence out of dataframes rows.
I have a dataframe with columns
df = df[['fullName', 'Location', 'College', 'Degree_location', 'Degree', 'Jobs', 'Company', 'Date', 'LocationJob', 'Avg_rounded']]
I'm trying to match each row of the column with a hardcoded sentence ie
name_placeholder = "My name is "
location_placeholder = "I live in "
college_placeholder = "I went to "
collloc_placeholder = " in "
degree_placeholder = "I have a degree in "
job_placeholder = "I have work as "
company_placeholder = " in companies "
date_placeholder = "I have work from "
jobloc_placeholder = "My job locations have been "
avgdistance_placeholder = "The average distance between my locations is "
placeholders = name_placeholder, location_placeholder, college_placeholder, collloc_placeholder, degree_placeholder, job_placeholder, company_placeholder, date_placeholder, jobloc_placeholder, avgdistance_placeholder
Where I expect it to end like "My name is [fullName]. I live in [Location]. I went to [College] " and so forth
If I use a for loop to access the rows of the dataframe ie:
for x in profiles:
for y in x:
print(y)
I got the expected results.
However, I'm trying to make sentences out of the dataframe.
I have tried
for x in profiles:
for y in x:
for pl, pr in zip(cycle(placeholders), y):
print(pl + pr)
But this gives me
My name is A
I live in d
I went to a
in m
I have a degree in
Why is the dataframe row printing each character instead of the full string ie [fullName]?
From my understanding, I'm combining the dataframe and placeholders list and I'm cycling through placeholders since it's a much shorter list than the dataframe.
Where am I going wrong?
Solution
Assuming profiles
is a row in df
, you don't need for y in x
as that is attempting to iterate every character in each string value in the row. Just use:
for x in profiles:
for pl, pr in zip(cycle(placeholders), x):
print(pl + pr)
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.