Pandas DataFrame:如何从自身引用多个行子集? [英] Pandas DataFrame: how to reference to multiple sub set of row from itself?

查看:49
本文介绍了Pandas DataFrame:如何从自身引用多个行子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个包含多个子集的数据帧.例如:DataFrame(data = a[1,2,3,4,5,6,7,8,9]).我想用 iloc[0,3] 和 iloc[6:9] 构建一个数据帧,结果是:DataFrame(data = a[1,2,3,6,7,8]).

I want to get a dataframe which included multiple subset from itself. For example: DataFrame(data = a[1,2,3,4,5,6,7,8,9]). I want build a dataframe with iloc[0,3] and iloc[6:9] which resulting: DataFrame(data = a[1,2,3,6,7,8]).

目前我正在这样做,它一直在进行数据复制并且非常慢:

Currently I am doing like this which is keep doing data copying and very slow:

if my_df is not None:                
    domain += 1
    new_domain = df.iloc[begin_iloc: begin_of_next_iloc]
    new_domain['domain'] = domain
    my_df = my_df.append(new_domain)
else:
    my_df = df.iloc[begin_iloc: begin_of_next_iloc]

推荐答案

您可以使用 numpy.r_ 用于连接索引:

You can use numpy.r_ for concanecate indices:

print (np.r_[0:3, 6:9])
[0 1 2 6 7 8]

print (df.iloc[np.r_[0:3, 6:9]])
   a
0  1
1  2
2  3
6  7
7  8
8  9

这篇关于Pandas DataFrame:如何从自身引用多个行子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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