KevOMalley743
KevOMalley743

Reputation: 581

Print pandas column name and cell value row wise

I need to split up a pandas dataframe by row into headed paragraphs. The column name is the heading and the cell value is the paragraph.

Example df (the actual df is much longer)

data = {'Title': ['Wormwood', 'Transmetropolitan', 'Y - The last man'],
    'Author': ['Ben Templesmith', 'Warren Ellis', 'Brian K. Vaughan'],
    'Abstract' :['Blah blah', 'Yadda yadda', 'Alas, poor Yorick! I knew him....']}
    
df = pd.DataFrame(data, columns = ['Title', 'Author', 'Abstract'])

my desired output is


Title: Wormwood

Author: Ben Templesmith

Abstract: Blah blah

Title: Transmetropolitan

Author: Warren Ellis

Abstract: Yadda yadda

Title: Y-the last man

Author: Brian K. Vaughan

Abstract: Alas, poor Yorick! I knew him....


Code I've tried

for i,j in zip(range(len(df.columns), range(len(df)):
    print(f'***{df.columns[i]}***\n  {df.iloc[j][j]}')
    i+=1
    j+=1

This kind of works but it seems really clunky and won't work on a longer more complex df.

For context, I'm basically trying to turn this df into a small booklet if that makes sense.

Upvotes: 1

Views: 1509

Answers (1)

jezrael
jezrael

Reputation: 862771

Use DataFrame.stack for reshape, then remove index repeated values by first reset_index with drop=True and set new columns names:

df1 = df.stack().reset_index(level=0, drop=True).rename_axis('a').reset_index(name='b')
print (df1)
          a                                  b
0     Title                           Wormwood
1    Author                    Ben Templesmith
2  Abstract                          Blah blah
3     Title                  Transmetropolitan
4    Author                       Warren Ellis
5  Abstract                        Yadda yadda
6     Title                   Y - The last man
7    Author                   Brian K. Vaughan
8  Abstract  Alas, poor Yorick! I knew him....

Then is necessary print in f-strings:

for a, b in df1.to_numpy():
    print (f'{a}: {b}')
    Title: Wormwood
Author: Ben Templesmith
Abstract: Blah blah
Title: Transmetropolitan
Author: Warren Ellis
Abstract: Yadda yadda
Title: Y - The last man
Author: Brian K. Vaughan
Abstract: Alas, poor Yorick! I knew him....

Upvotes: 2

Related Questions