'Show Pandas Dataframe in for loop
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!
Solution 1:[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)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mahmoud K. |
Solution 2 | Hrvoje |