通过 pandas 中的索引选择行的多个部分 [英] Select multiple sections of rows by index in pandas

查看:55
本文介绍了通过 pandas 中的索引选择行的多个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有GPS路径和某些属性的大型DataFrame.我需要分析的部分路径.我只想将那些部分子集到一个新的DataFrame中.我当时可以将一个子集作为子集,但其想法是将它们全部都拥有并具有原始索引.

I have large DataFrame with GPS path and some attributes. A few sections of the path are those which I need to analyse. I would like to subset only those sections to a new DataFrame. I can subset one section at the time but the idea is to have them all and to have an original index.

问题类似于:

import pandas as pd 
df = pd.DataFrame({'A':[0,1,2,3,4,5,6,7,8,9],'B':['a','b','c','d','e','f','g','h','i','j']},
                  index=range(10,20,))

我想得到类似的东西

cdf = df.loc[[11:13] & [17:20]] # SyntaxError: invalid syntax

期望的结果:

    A  B
11  1  b
12  2  c
13  3  d
17  7  h
18  8  i
19  9  j

我知道使用cdf = df.loc[[11,12,13,17,18,19],:]进行示例很容易,但是在最初的问题中,我有成千上万的行,并且某些条目已被删除,因此列出点不是一个选择.

I know the example is easy with cdf = df.loc[[11,12,13,17,18,19],:] but in the original problem I have thousands of lines and some entries already removed, so listing points is rather not an option.

推荐答案

使用 concat :

cdf = pd.concat([df.loc[11:13], df.loc[17:20]])
print (cdf)
    A  B
11  1  b
12  2  c
13  3  d
17  7  h
18  8  i
19  9  j

使用range的另一种解决方案:

Another solution with range:

cdf = df.ix[list(range(11,14)) + list(range(17,20))]
print (cdf)
    A  B
11  1  b
12  2  c
13  3  d
17  7  h
18  8  i
19  9  j

这篇关于通过 pandas 中的索引选择行的多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆