首页 > 解决方案 > Python:如何显示数据框中的数据行,然后再次提示用户输入下一批行

问题描述

我想知道如何编写一个循环,每次从数据框中显示一行数据时提示用户。如果用户回答“是”,该函数将显示该行数据并重新提示用户,直到没有更多行为止。如果用户回答“否”,则循环应该中断。

标签: pythondataframeinputprinting

解决方案


import pandas as pd
df = pd.DataFrame(pd.np.random.rand(10,3))
for ind, row in df.iterrows():
    ans = input("Would you like to print a row? ")
    if  ans == 'yes':
        print("Row : {!r}".format(row.tolist()))
    elif ans == 'no':
        break
    else:
        print("Omit row # {}".format(ind))

推荐阅读