Reputation: 971
I have a numpy array as following:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
The array is called myArray, and I perform two indexing operations on the 2D array and get following results:
In[1]: a1 = myArray[1:]
a1
Out[1]:array([[3, 4],
[5, 6],
[7, 8]])
In[2]: a2 = myArray[:-1]
a2
Out[2]:array([[1, 2],
[3, 4],
[5, 6]])
Now, I have the same data in the form of a pandas df in two columns, let the data frame be df
x y
0 1 2
1 3 4
3 5 6
4 7 8
How to do the equivalent indexing/ slicing on two columns to get the same results as above for a1 and a2.
Upvotes: 3
Views: 24036
Reputation: 215107
Use iloc
:
df.iloc[1:]
# x y
#1 3 4
#3 5 6
#4 7 8
df.iloc[:-1]
# x y
#0 1 2
#1 3 4
#3 5 6
Use head/tail
:
df.head(-1) # equivalent to df.iloc[:-1]
# x y
#0 1 2
#1 3 4
#3 5 6
df.tail(-1) # equivalent to df.iloc[1:]
# x y
#1 3 4
#3 5 6
#4 7 8
Upvotes: 1