user3494047 user3494047 - 2 months ago 7
Python Question

python pandas elegant dataframe access rows 2:end

I have a dataframe,

dF = pd.DataFrame(X)
where X is a numpy array of doubles. I want to remove the last row from the dataframe. I know for the first row I can do something like this
dF.ix[1:]
. I want to do something similar for the last row. I know in matlab you could do something like this
dF[1:end-1]
. What is a good and readable way to do this with pandas?

The end goal is to achieve this:
first matrix

1 2 3
4 5 6
7 8 9


second matrix

a b c
d e f
g h i


now get rid of first row of first matrix and last row of second matrix and horizontally concatentate them like so:

4 5 6 a b c
7 8 9 d e f


done. In matlab a = firstMatrix. b = secondMatrix.
c = [a[2:end] b[1:end-1]]
where c is the resulting matrix.

Answer

you can do it this way:

In [129]: df1
Out[129]:
   c1  c2  c3
0   1   2   3
1   4   5   6
2   7   8   9

In [130]: df2
Out[130]:
  c1 c2 c3
0  a  b  c
1  d  e  f
2  g  h  i

In [131]: df1.iloc[1:].reset_index(drop=1).join(df2.iloc[:-1].reset_index(drop=1), rsuffix='_2')
Out[131]:
   c1  c2  c3 c1_2 c2_2 c3_2
0   4   5   6    a    b    c
1   7   8   9    d    e    f

Or a pure NumPy solution:

In [132]: a1 = df1.values

In [133]: a1
Out[133]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]], dtype=int64)

In [134]: a2 = df2.values

In [135]: a2
Out[135]:
array([['a', 'b', 'c'],
       ['d', 'e', 'f'],
       ['g', 'h', 'i']], dtype=object)

In [136]: a1[1:]
Out[136]:
array([[4, 5, 6],
       [7, 8, 9]], dtype=int64)

In [137]: a2[:-1]
Out[137]:
array([['a', 'b', 'c'],
       ['d', 'e', 'f']], dtype=object)

In [138]: np.concatenate((a1[1:], a2[:-1]), axis=1)
Out[138]:
array([[4, 5, 6, 'a', 'b', 'c'],
       [7, 8, 9, 'd', 'e', 'f']], dtype=object)
Comments