Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Visually Pleasing DataFrames in For Loop in Jupyter Notebook Pandas

Say I have these two Data Frames in a list,

sm = pd.DataFrame([["Forever", 'BenHarper'],["Steel My Kisses", 'Kack Johnson'],\
                  ["Diamond On the Inside",'Xavier Rudd'],[ "Count On Me", "Bruno Mars"]],\
                   columns=["Song", "Artist"])

pm = pd.DataFrame([["I am yours", 'Jack Johnson'],["Chasing Cars", 'Snow Patrol'],\
                  ["Kingdom Comes",'Cold Play'],[ "Time of your life", "GreenDay"]],\
                   columns=["Song", "Artist"])

df_list = [sm,pm]

Now, when I want to print both data frames while iterating, I get something like this,

for i in df_list:
    print(i)

Result,

                  Song        Artist
0                Forever     BenHarper
1        Steel My Kisses  Kack Johnson
2  Diamond On the Inside   Xavier Rudd
3            Count On Me    Bruno Mars
                Song        Artist
0         I am yours  Jack Johnson
1       Chasing Cars   Snow Patrol
2      Kingdom Comes     Cold Play
3  Time of your life      GreenDay

However, when we do df_list[0] it prints in a pleasing tabular manner,

enter image description here

Can I get that same way in a visually pleasing way when I loop through the list and print Data Frame?? I have been searching and no luck yet. Any ideas how to do that?

(Sorry, if this is something that is normal in python, as I am new to Python and Jupyter, visually pleasing ones make me feel happy to see)

like image 589
user9431057 Avatar asked Jul 11 '18 15:07

user9431057


People also ask

How do you display a Dataframe in Jupyter notebook?

You can visualize a pandas dataframe in Jupyter notebooks by using the display(<dataframe-name>) function. The display() function is supported only on PySpark kernels. The Qviz framework supports 1000 rows and 100 columns.

What is the best way to iterate through a Dataframe?

Vectorization is always the first and best choice. You can convert the data frame to NumPy array or into dictionary format to speed up the iteration workflow. Iterating through the key-value pair of dictionaries comes out to be the fastest way with around 280x times speed up for 20 million records.


1 Answers

You can use this:

from IPython.display import display

for i in df_list:
    display(i)

enter image description here

Learn more tricks about rich and flexible formatting at Jupyter Notebook Viewer

like image 180
nealmcb Avatar answered Oct 06 '22 14:10

nealmcb