0

I have a list of attachments (.pdf files) in a folder and another dataframe with name and values. Each name may have multiple rows if the name has multiple values.

I want to print out the values corresponding to each name in a loop.

df = pd.DataFrame(data = {'Name': ["Peter", "Peter", "Peter", "Jack", "Jack"], 
                          'Value': ['a','b','c','a','nan']})

I tried these two loops:

for i in df0['Name'].unique():
    print(df['Values'][df['Name'] == i], '\n')

for i,j in df.groupby('Name'):
    print(df['Values'][df['Name'] == i]) 

where each "printout" is a series. Instead I want to print out each data value for each name but "grouped".

For example:

Peter:
a
b
c

next

Jack:
a

1 Answer 1

1

If want for each loop one column DataFrame with column name like group name use Series.to_frame:

for i,j in df.groupby('Name'):
    print(j['Value'].to_frame(i))

Or:

for i,j in df.groupby('Name')['Value']:
    print(j.to_frame(i))
Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to print out each value as string? I get pandas.core.frame.DataFrame? Because when I put the values in a list, the values come out as string.
@Mataunited18 - Do you think print(j['Value'].to_frame(i).to_string(index=False)) ?
all the values get bundled together in one string. Is it possible that each value get printed out as string instead of combining them together?
@Mataunited18 0 What about change print(j['Value'].to_frame(i)) to s = j['Value'] and then for val in s: print (val) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.