Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring a few columns to the front in a huge Panda DataFrame

Tags:

python

pandas

I have a Panda series with 1000 columns. 
What’s the easiest way to bring just three of these columns to the front (any three) lets say column no 10, 30 and 1000 with the rest of the columns making way for them.

I know I can do:

Df = Df.iloc[:,[10, 30, 1000]]

However, this just gives me three columns

Columns 

10    30   50

However I want an output like this:

10    30   1000   1      2       3      4     5     6     7      8     999

That is, I want to bring three columns to the front and have the rest of the columns be moved back to make space for them. What’s the simplest way to do that? As I would need to do that quite frequently in my database.

Regards

like image 940
Saif Ul haq Avatar asked Jun 03 '19 10:06

Saif Ul haq


2 Answers

Here's one way using sorted with a key:

first = [10, 30, 1000]
df[sorted(df.columns, key=lambda x: x not in first)]

Example:

df = pd.DataFrame(columns = list(range(10)))
first = [2,3,9]
print(df[sorted(df.columns, key=lambda x: x not in first)].columns)
# Int64Index([2, 3, 9, 0, 1, 4, 5, 6, 7, 8], dtype='int64')
like image 140
yatu Avatar answered Nov 11 '22 22:11

yatu


Here is another approach using numpy.r_ and Index.difference:

import numpy as np

# Setup
np.random.seed(0)
df = pd.DataFrame(np.random.randn(10, 7))
cols = [2, 5, 6]

df[np.r_[cols, df.columns.difference(cols)]]

[out]

          2         5         6         0         1         3         4
0  0.978738 -0.977278  0.950088  1.764052  0.400157  2.240893  1.867558
1  0.410599  0.761038  0.121675 -0.151357 -0.103219  0.144044  1.454274
2  1.494079 -0.854096 -2.552990  0.443863  0.333674 -0.205158  0.313068
3 -0.742165  0.045759 -0.187184  0.653619  0.864436  2.269755 -1.454366
4  0.154947 -1.980796 -0.347912  1.532779  1.469359  0.378163 -0.887786
5  1.202380 -1.048553 -1.420018  0.156349  1.230291 -0.387327 -0.302303
6 -0.509652  0.777490 -1.613898 -1.706270  1.950775 -0.438074 -1.252795
7  0.386902 -0.028182  0.428332 -0.212740 -0.895467 -0.510805 -1.180632
8 -0.634322 -0.359553 -0.813146  0.066517  0.302472 -0.362741 -0.672460
9 -0.401781 -0.907298  0.051945 -1.726283  0.177426 -1.630198  0.462782
like image 30
Chris Adams Avatar answered Nov 11 '22 22:11

Chris Adams