Pandas DataFrame: how to reference to multiple sub set of row from itself?

笑着哭i 提交于 2021-02-17 04:56:06

问题


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]

回答1:


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


来源:https://stackoverflow.com/questions/40783302/pandas-dataframe-how-to-reference-to-multiple-sub-set-of-row-from-itself

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!