Python,用于根据值拾取行

2024-06-01 08:56:25 发布

您现在位置:Python中文网/ 问答频道 /正文

一个数据帧,我想通过列中的值来选择它。在这种情况下,“报告”的行数介于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

上面生成了两组相同的错误结果(有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

出了什么问题?多谢各位


Tags: namedfdatamollypddavidreportsjack
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

对于bitwise AND,您需要&,但更好的方法是使用^{}

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

相关问题 更多 >