python - Python:如何显示数据框中的数据行,然后再次提示用户输入下一批行
问题描述
我想知道如何编写一个循环,每次从数据框中显示一行数据时提示用户。如果用户回答“是”,该函数将显示该行数据并重新提示用户,直到没有更多行为止。如果用户回答“否”,则循环应该中断。
解决方案
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))
推荐阅读
- c++ - Qt Creator 调试器日志窗口不必要地转义了许多字符使输出分散注意力
- java - 是否可以突出显示方法、for 循环、while 循环等的背景?
- github - 用于网站重建的 GitHub 存储库
- annotations - 如何在 Protege 中添加图像
- google-maps - 进行图像搜索?
- c# - C# 附加信息:字段选择 ACCOUNT 的子列表,无法创建 n
- c - 尝试制作结构函数时出错
- elixir - Elixir 将模块的引用作为函数参数传递
- c# - selenium c# - “懒惰”驱动程序.FindElements(By..)
- python - 简单的返回函数,还有什么更 Pythonic 的?