在for循环中显示Pandas数据框

qmelpv7a  于 2023-04-04  发布在  其他
关注(0)|答案(3)|浏览(93)

我有一个for循环,在其中我构建了一个pandas dataframe,每次循环开始时,dataframe都会更新。我想做的是在再次更新之前描述这个表并再次显示它,当然是用更新的值。如果我想在每次迭代中绘制一些值,并且这些图会一个接一个地显示出来,那么就可以这样做。但我似乎不能为数据框或基本表做同样的事情。

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!
b09cbbtk

b09cbbtk1#

不清楚你是否在笔记本电脑中工作,但我认为你正在寻找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)
laawzig2

laawzig22#

你可以试试这个:

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)
ogq8wdun

ogq8wdun3#

这段代码允许“清理”Jupyter notebook单元格,并允许更新/刷新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)

相关问题