使用python 3编码和 熊猫版0.18.1
我正试图让我的程序更动态,给用户选项,从数据框过滤数据。
我的问题是:
1)如何使我的用户选择可用于数据帧中的筛选?
2)有更好的方法吗?带函数或类的Mabye?
假设我的df如下:
df.dtypes
PIID object
fy object
zone object
如果fy分组:
df.groupby('fy').PIID.count()
fy
2014 38542
2015 33629
2016 32789
如果区域分组:
df.groupby('zone').PIID.count()
AZW - Acquisition Zone West 3909
NAZ - Northern Acquisition Zone 1167
SAZ - Southern Acquisition Zone 2983
通常,我可以通过执行以下操作创建一个带有筛选器的新数据帧:
year = df['fy'] == '2016'
zone = df['zone'] == 'AZW - Acquisition Zone West'
newdf = df[year & zone]
但是,我如何通过提供用户选项使其更具动态性呢?
在这一点上,我为用户提供了一些带有布尔值的fy选项:
print ('What is the interested year?')
print ('1. 2014')
print ('2. 2015')
print ('3. 2016')
year = input('> ')
if year == '1':
year1 = df['fy'] == '2014'
elif year == '2':
year2 = df['fy'] == '2015'
还有一些布尔人的区域:
print ('What is the interested zone?')
print ('1. AZW - Acquisition Zone West')
print ('2. NAZ - Northern Acquisition Zone')
print ('3. SAZ - Southern Acquisition Zone')
zone = input('> ')
if zone == '1':
zone1 = df['zones'] == 'AZW - Acquisition Zone West'
elif zone == '2':
zone2 = df['zones'] == 'Northern Acquisition Zone'
此时我不知道如何接收用户的选择
newdf = df[choice1 & choice2]
其中选择1是年份,选择2是区域。
提前感谢您的帮助!
目前没有回答
相关问题 更多 >
编程相关推荐