小编LOR*_*121的帖子

Python - 如何使用户输入不区分大小写?

我是 Python 的新手,真的可以在这方面使用一些帮助。我想创建一个函数来过滤我想要打开的文件以及具体的月份和日期。这样,用户需要输入他们想要在哪个特定月份或日期分析哪个城市(文件)。但是,我希望用户能够输入不区分大小写的内容。例如,用户可以输入 'chicago'/'CHICAGO"/"ChIcAgO" 并且它仍然为您提供正确的输出而不是错误处理消息。这是我使用的代码:

def get_filters ():

    city_options = ['Chicago','New York City','Washington']
    month_options = ['January','February','March','April','May','June','All']
    day_options = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','All']
    while True:
        try:
            city = city_options.index(input('\nInsert name of the city to analyze! (Chicago, New York City, Washington)\n'))
            month = month_options.index(input('\nInsert month to filter by or "All" to apply no month filter! (January, February, etc.)\n'))
            day = day_options.index(input('\nInsert day of the week to filter by or "All" to apply no day filter! (Monday, Tuesday, etc.)\n'))
            return city_options[city].lower(), month_options[month].lower(), day_options[day].lower()
        except ValueError:
            print …
Run Code Online (Sandbox Code Playgroud)

python user-input case-insensitive

3
推荐指数
2
解决办法
3万
查看次数

Python:从数据透视表熊猫数据框创建条形图

我是 python 新手,想知道如何在我使用数据透视表函数创建的数据上创建条形图。

#Create a pivot table for handicaps count calculation for no-show people based on their gender 
pv = pd.pivot_table(df_main, values=['hipertension','diabetes','alcoholism'], 
                     columns='status',index='gender',aggfunc=np.sum)
#Reshape the pivot table for easier calculation 

data_pv = pv.unstack().unstack('status').reset_index().rename(columns={'level_0':'category','No-Show':'no_show', 'Show-Up':'show_up'})

data_pv['no_show_prop'] = (data_pv['no_show']/
                          (data_pv['no_show']+data_pv['show_up']))*100
data_pv
Run Code Online (Sandbox Code Playgroud)

结果:

status  category    gender  no_show show_up no_show_prop
0   alcoholism      F        308       915     25.183974
1   alcoholism      M        369      1768     17.267197
2   diabetes        F        1017     4589     18.141277
3   diabetes        M        413      1924     17.672229
4   hipertension    F        2657    12682     17.321859
5   hipertension    M        1115 …
Run Code Online (Sandbox Code Playgroud)

python pivot-table matplotlib pandas

1
推荐指数
2
解决办法
1万
查看次数