过滤Pandas中的记录python - 语法错误

时间:2015-12-23 01:55:32

标签: python csv pandas

我有一个像这样的pandas数据框:

       duration  distance  speed  hincome  fi_cost     type
0           359      1601      4        3    40.00  cycling
1           625      3440      6        3    86.00  cycling
2           827      4096      5        3   102.00  cycling
3          1144      5704      5        2   143.00  cycling

如果我使用以下内容,我会导出一个新的csv,它只提取距离小于5000的记录。

distance_1 = all_results[all_results.distance < 5000]
distance_1.to_csv('./distance_1.csv',",")

现在,我想导出一个值为5001到10000的csv。我似乎无法正确使用语法......

distance_2 = all_results[10000 > all_results.distance < 5001]
distance_2.to_csv('./distance_2.csv',",")

2 个答案:

答案 0 :(得分:3)

不幸的是,由于Python链接比较的工作原理,我们不能使用50&lt; x&lt;当x是一些类似矢量的数量时,100语法。你有几个选择。

你可以创建两个布尔系列并使用&来组合它们:

>>> all_results[(all_results.distance > 3000) & (all_results.distance < 5000)]
   duration  distance  speed  hincome  fi_cost     type
1       625      3440      6        3       86  cycling
2       827      4096      5        3      102  cycling

使用between创建一个布尔系列,然后使用它进行索引(请注意,默认情况下它是包含的):

>>> all_results[all_results.distance.between(3000, 5000)] # inclusive by default
   duration  distance  speed  hincome  fi_cost     type
1       625      3440      6        3       86  cycling
2       827      4096      5        3      102  cycling

或者最后你可以使用.query

>>> all_results.query("3000 < distance < 5000")
   duration  distance  speed  hincome  fi_cost     type
1       625      3440      6        3       86  cycling
2       827      4096      5        3      102  cycling

答案 1 :(得分:0)

            #container2 input[type=submit]{
            border: 1px solid black;
            width: 25%;
            height: 40px;
            margin-left: 68%;
            background-color: #C4D8E2;
        }