numpy 如何在Python中显示基于用户的五行数据?

v8wbuo2f  于 2022-11-10  发布在  Python
关注(0)|答案(1)|浏览(144)
df = pd.read_csv(CITY_DATA[city])

def user_stats(df,city):
    """Displays statistics of users."""

    print('\nCalculating User Stats...\n')

    start_time = time.time()

    print('User Type Stats:')
    print(df['User Type'].value_counts())

    if city != 'washington':
        print('Gender Stats:')
        print(df['Gender'].value_counts())

        print('Birth Year Stats:')

        most_common_year = df['Birth Year'].mode()[0]
        print('Most Common Year:',most_common_year)

        most_recent_year = df['Birth Year'].max()
        print('Most Recent Year:',most_recent_year)

        earliest_year = df['Birth Year'].min()
        print('Earliest Year:',earliest_year)

    print("\nThis took %s seconds." % (time.time() - start_time))
    print('-'*40)

我想在第一步询问用户:“您想要查看前5行数据吗?”如果他输入yes,系统将显示前5行,然后再次询问用户“是否要查看下5行数据?”然后他回答是,它会显示接下来的5个数据。我要一直问,直到他说不。
提示:
-我们将根据位置显示数据。也就是说,我们将在第一次尝试中显示前5个数据,然后显示第二个“是”的第二个5个数据,因此我们需要跟踪这一点。多么?(我使用了START_LOC变量)
-请检查iloc功能。它根据位置返回 Dataframe 。例如,df.iloc[0:5]将返回前5行数据。
我可以使用以下代码来完成此操作吗:

view_data = input('\nWould you like to view 5 rows of individual trip data? Enter yes or no\n')
start_loc = 0
while (?????):
    print(df.iloc[????:????])
    start_loc += 5
    view_display = input("Do you wish to continue?: “).lower()
vlurs2pr

vlurs2pr1#

像这样的吗?
1.在iloc中使用您的变量startloc
1.只需为for循环分配一个布尔值,如果用户键入“no”,则将其设置为FALSE。

view_data = input('\nWould you like to view 5 rows of individual trip data? Enter yes or no\n')
start_loc = 0
keep_asking = True
while (keep_asking):
    print(df.iloc[start_loc:start_loc + 5])
    start_loc += 5
    view_display = input("Do you wish to continue?: ").lower()
    if view_display == "no": 
        keep_asking = False

相关问题