3

I have a pandas DataFrame with 400 columns and 100 rows which I would like to display as an image. The instrument that generates these data does so in a way requiring every second row to be reversed for it to be displayed properly.

How can I reverse specific rows in pandas or with numpy if converted to a 2D np.array (i.e. row 1, 3, 5, 7...len(df)?

3 Answers 3

11

If your application is mainly image processing, a pandas DataFrame is probably overkill. Here's an example of one way to reverse every other row of a numpy array. It operates in-place:

In [656]: a
Out[656]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])

In [657]: a[1::2, :] = a[1::2, ::-1]

In [658]: a
Out[658]: 
array([[ 0,  1,  2,  3],
       [ 7,  6,  5,  4],
       [ 8,  9, 10, 11],
       [15, 14, 13, 12],
       [16, 17, 18, 19],
       [23, 22, 21, 20]])
Sign up to request clarification or add additional context in comments.

2 Comments

could you elaborate on how this achieves the goal exactly?
It's doing an inplace assignment. a[1::2, :] means give me every other row (starting from 1) in axis 0. Then give me every single row along the axis 1. Then we assign this to the same thing except when we get every single row of axis 1, we step by -1 which gives us those rows backwards. So every other row gets replaced with the flipped version of that. The important thing to know is that the slice there is start:end:step. The command is mostly playing with the stepping to get the even rows and the reverse them.
4

Setup

import pandas as pd

df = pd.DataFrame([range(5) for _ in range(10)],
                  index=list('abcdefghij'),
                  columns=list('abcde'))

print df

   a  b  c  d  e
a  0  1  2  3  4
b  0  1  2  3  4
c  0  1  2  3  4
d  0  1  2  3  4
e  0  1  2  3  4
f  0  1  2  3  4
g  0  1  2  3  4
h  0  1  2  3  4
i  0  1  2  3  4
j  0  1  2  3  4

Solution and Explanation

df.iloc[1::2, :] = df.iloc[1::2, ::-1].values
#       ^  ^                        ^  
#       |  |                  Reverse
#  Start   |
# Every other row

Demonstration

print df

   a  b  c  d  e
a  0  1  2  3  4
b  4  3  2  1  0
c  0  1  2  3  4
d  4  3  2  1  0
e  0  1  2  3  4
f  4  3  2  1  0
g  0  1  2  3  4
h  4  3  2  1  0
i  0  1  2  3  4
j  4  3  2  1  0

Comments

1
df = pd.DataFrame([range(4)], columns=list('ABCD'), index=range(5))

>>> df
   A  B  C  D
0  0  1  2  3
1  0  1  2  3
2  0  1  2  3
3  0  1  2  3
4  0  1  2  3

Create a mask where the rows you want to reverse are True (e.g. Even = False, Odd = True), the use loc to locate those rows and reverse their values (::-1 is a common way to reverse a list in Python).

mask = [i % 2 == 1 for i in range(len(df))]  
df.loc[mask] = df.loc[mask, ::-1].values

>>> df
   A  B  C  D
0  0  1  2  3
1  3  2  1  0
2  0  1  2  3
3  3  2  1  0
4  0  1  2  3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.