Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

zip cycle a list is printing characters instead of string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading