Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 要求用户继续查看接下来的5行数据_Python_Pandas - Fatal编程技术网

Python 要求用户继续查看接下来的5行数据

Python 要求用户继续查看接下来的5行数据,python,pandas,Python,Pandas,我试图在每次([:5]、[5:10]、[10:15]等)都不写循环的情况下获取接下来的5个数据行。我有一些想法,但我在这个问题上停留了一段时间,有什么方法可以让用户展示下一个5 def raw_data(df): ask_user = input('Would you like to view more raw data for the city selected? \nPrint yes or no: ') while True: if ask_user == 'yes':

我试图在每次([:5]、[5:10]、[10:15]等)都不写循环的情况下获取接下来的5个数据行。我有一些想法,但我在这个问题上停留了一段时间,有什么方法可以让用户展示下一个5

def raw_data(df):
ask_user = input('Would you like to view more raw data for the city selected? \nPrint yes or no: ')
while True:
    if ask_user == 'yes':
        return df.iloc[:5]
    else:
        break

添加每次函数运行时累积的变量。(我们称之为
运行
)在函数外部初始化它,并在函数内部添加一个。从这里开始,乘以5得到你的射程

runs = 0
def raw_data(df):
    while True: 
        ask_user = input('Would you like to view more raw data for the city selected? \nPrint yes or no: ') 
        if ask_user == 'yes': 
            runs += 1 #Adds 1 to current value, same as runs = runs + 1
            return df.iloc[(x-1)*5:x*5]
        elif ask_user == 'no':
            return

请正确格式化/缩进代码,因为Python对空格敏感。对于5计数器,使用变量跟踪范围并添加到它。如果len(df)不能被5I整除,您可以添加一个控件。我不了解这个循环的行为-什么控制它?若用户说不,你们就去无穷远?那个对我不起作用。我仍然希望它询问用户是否愿意继续下5行。@WalterSolares抱歉,我是根据您的代码编写的,忘记将输入移动到循环中。已编辑并修复。@EvgenyPogrebnyak如果用户说“否”,则有一个
break
语句退出循环。