Python,Pandas根据值

时间:2018-03-16 07:32:02

标签: python pandas dataframe

数据框,我想通过列中的值选择一些数据框。在这种情况下,行报告'在10~31之间。

import pandas as pd

data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy', 'Daisy', 'River', 'Kate', 'David', 'Jack', 'Nancy'], 
    'month of entry': ["20171002", "20171206", "20171208", "20171018", "20090506", "20171128", "20101216", "20171230", "20171115", "20171030", "20171216"],
    'reports': [14, 24, 31, 22, 34, 6, 47, 2, 14, 10, 8]}
df = pd.DataFrame(data)

df_4 = df[(df.reports >= 10) | (df.reports <= 31)]
df_5 = df.query('reports >= 10 | reports <= 31')

print df_4
print df_5

上面生成了2组相同的错误结果(47就是!):

   month of entry   name  reports
0        20171002  Jason       14
1        20171206  Molly       24
2        20171208   Tina       31
3        20171018   Jake       22
4        20090506    Amy       34
5        20171128  Daisy        6
6        20101216  River       47
7        20171230   Kate        2
8        20171115  David       14
9        20171030   Jack       10
10       20171216  Nancy        8

出了什么问题?谢谢。

2 个答案:

答案 0 :(得分:2)

&需要bitwise AND,但最好使用between

df1 = df[(df.reports >= 10) & (df.reports <= 31)]

或者:

df1 = df[df.reports.between(10,31)] 
print (df1)
  month of entry   name  reports
0       20171002  Jason       14
1       20171206  Molly       24
2       20171208   Tina       31
3       20171018   Jake       22
8       20171115  David       14
9       20171030   Jack       10

<强>详细

print ((df.reports >= 10) & (df.reports <= 31))
0      True
1      True
2      True
3      True
4     False
5     False
6     False
7     False
8      True
9      True
10    False
Name: reports, dtype: bool

答案 1 :(得分:2)

import pandas as pd

data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy', 'Daisy', 'River', 'Kate', 'David', 'Jack', 'Nancy'], 
    'month of entry': ["20171002", "20171206", "20171208", "20171018", "20090506", "20171128", "20101216", "20171230", "20171115", "20171030", "20171216"],
    'reports': [14, 24, 31, 22, 34, 6, 47, 2, 14, 10, 8]}
df = pd.DataFrame(data)
df_4 = df[(df.reports >= 10) & (df.reports <= 31)]   #Use '&' instead of '|'
print df_4

<强>输出:

  month of entry   name  reports
0       20171002  Jason       14
1       20171206  Molly       24
2       20171208   Tina       31
3       20171018   Jake       22
8       20171115  David       14
9       20171030   Jack       10