0

I have a for-loop in which I build a pandas dataframe and everytime the loop starts over, the dataframe is updated. What I would like to do is to depict this table before being updated again and showing it again, of course with updated values. It is possible to do such if I was going to plot some value in each iteration and the plots would show up one after each other. But I seem not to be able to do the same for a dataframe or basically table.

df = pd.DataFrame(index = x, columns=y)
for i in range(df.shape[0]):
    for j in range(df.shape[1]):
        if condition is True:
            df.iloc[i,j] = 'True'
        else:
            df.iloc[i,j] = 'False'

    Show df!
Arad Haselirad
  • 314
  • 2
  • 13
  • Please clarify: you have trouble with multiple plots in the same jupyter cell? Did you saw [Make more than one chart in same IPython Notebook cell](https://stackoverflow.com/questions/16392921/make-more-than-one-chart-in-same-ipython-notebook-cell])? – Alex Yu Feb 14 '19 at 21:19
  • are you trying create some sort of "over time" type visual? – MattR Feb 14 '19 at 21:28
  • No, My problem is not with visualizing plots. I was just trying to say that I do not have any problem visualizing a plot in a for loop. But I am trying to visualize a table every time the loop starts over. The table is a pandas dataframe. – Arad Haselirad Feb 14 '19 at 22:23

3 Answers3

1

It's unclear whether you are working inside a notebook or not. but I think your are looking for display:

from IPython.display import display

df = pd.DataFrame(index = x, columns=y)
for i in range(df.shape[0]):
    for j in range(df.shape[1]):
        if condition is True:
            df.iloc[i,j] = 'True'
        else:
            df.iloc[i,j] = 'False'

    display(df)
Mahmoud
  • 9,729
  • 1
  • 36
  • 47
0

You can try this:

from IPython.display import display

    df = pd.DataFrame(index = x, columns=y)
    for i in range(df.shape[0]):
        for j in range(df.shape[1]):
            print("Values of i and j:",i,j)
            print("DataFrame before update:")
            display(df)
            if condition is True:
                df.iloc[i,j] = 'True'
            else:
                df.iloc[i,j] = 'False'
            print("DataFrame after update:")
            display(df)
    
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
0

This code allow to "clean" the Jupyter notebook cell, and allow to update/refresh the dataframe:

from IPython.display import display, clear_output

df = pd.DataFrame(index = x, columns=y)
for i...:
    for j...:
        # Update df...
        clear_output(wait=True)
        display(df)
mountrix
  • 1,126
  • 15
  • 32