问题
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