Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing unnecessary row when re-indexing in pandas

Tags:

python

pandas

I have a panda data frame that looks like this and can be copy pasted in with pd.read_clipboard() :

     0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
0    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1    1   0   3   2   5   4   7   6   9   8  11  10  13  12  15  14
2    2   3   0   1   6   7   4   5  10  11   8   9  14  15  12  13
3    3   2   1   0   7   6   5   4  11  10   9   8  15  14  13  12
4    4   5   6   7   0   1   2   3  12  13  14  15   8   9  10  11
5    5   4   7   6   1   0   3   2  13  12  15  14   9   8  11  10
6    6   7   4   5   2   3   0   1  14  15  12  13  10  11   8   9
7    7   6   5   4   3   2   1   0  15  14  13  12  11  10   9   8
8    8   9  10  11  12  13  14  15   0   1   2   3   4   5   6   7
9    9   8  11  10  13  12  15  14   1   0   3   2   5   4   7   6
10  10  11   8   9  14  15  12  13   2   3   0   1   6   7   4   5
11  11  10   9   8  15  14  13  12   3   2   1   0   7   6   5   4
12  12  13  14  15   8   9  10  11   4   5   6   7   0   1   2   3
13  13  12  15  14   9   8  11  10   5   4   7   6   1   0   3   2
14  14  15  12  13  10  11   8   9   6   7   4   5   2   3   0   1
15  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0

When i reindex it creates an extra 2 which causes me issues as my code to read the index gives an error:

In [6025]: lookuptable.reindex(lookuptable[2])                                                                                                                                                                                                  
Out[6025]: 
     0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
2                                                                 
2    2   3   0   1   6   7   4   5  10  11   8   9  14  15  12  13
3    3   2   1   0   7   6   5   4  11  10   9   8  15  14  13  12
0    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1    1   0   3   2   5   4   7   6   9   8  11  10  13  12  15  14
6    6   7   4   5   2   3   0   1  14  15  12  13  10  11   8   9
7    7   6   5   4   3   2   1   0  15  14  13  12  11  10   9   8
4    4   5   6   7   0   1   2   3  12  13  14  15   8   9  10  11
5    5   4   7   6   1   0   3   2  13  12  15  14   9   8  11  10
10  10  11   8   9  14  15  12  13   2   3   0   1   6   7   4   5
11  11  10   9   8  15  14  13  12   3   2   1   0   7   6   5   4
8    8   9  10  11  12  13  14  15   0   1   2   3   4   5   6   7
9    9   8  11  10  13  12  15  14   1   0   3   2   5   4   7   6
14  14  15  12  13  10  11   8   9   6   7   4   5   2   3   0   1
15  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
12  12  13  14  15   8   9  10  11   4   5   6   7   0   1   2   3
13  13  12  15  14   9   8  11  10   5   4   7   6   1   0   3   2

As you can see it created an extra 2 on the top of the index with nothing in the row. I don't need that row at all i want it to look like this:

     0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
2    2   3   0   1   6   7   4   5  10  11   8   9  14  15  12  13
3    3   2   1   0   7   6   5   4  11  10   9   8  15  14  13  12
0    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1    1   0   3   2   5   4   7   6   9   8  11  10  13  12  15  14
6    6   7   4   5   2   3   0   1  14  15  12  13  10  11   8   9
7    7   6   5   4   3   2   1   0  15  14  13  12  11  10   9   8
4    4   5   6   7   0   1   2   3  12  13  14  15   8   9  10  11
5    5   4   7   6   1   0   3   2  13  12  15  14   9   8  11  10
10  10  11   8   9  14  15  12  13   2   3   0   1   6   7   4   5
11  11  10   9   8  15  14  13  12   3   2   1   0   7   6   5   4
8    8   9  10  11  12  13  14  15   0   1   2   3   4   5   6   7
9    9   8  11  10  13  12  15  14   1   0   3   2   5   4   7   6
14  14  15  12  13  10  11   8   9   6   7   4   5   2   3   0   1
15  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
12  12  13  14  15   8   9  10  11   4   5   6   7   0   1   2   3
13  13  12  15  14   9   8  11  10   5   4   7   6   1   0   3   2

I tried lookuptable.droplevel(1) and lookuptable.droplevel(0), neither which worked. Any help would be appreciated if you can help me create the reindex to look like the sample i posted above. Thanks in advance.

like image 989
oppressionslayer Avatar asked Dec 18 '25 07:12

oppressionslayer


2 Answers

It's just lookups[2] has a name, namely 2. So it puts the number 2 there for you to know that the new index has a name. It's not an extra row, as you can see with lookups.reindex(lookups[2]).shape.

If you really really don't like that number 2, just pass the numpy array to reindex:

lookups.reindex(lookups[2].values)

Output

    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
2    2   3   0   1   6   7   4   5  10  11   8   9  14  15  12  13
3    3   2   1   0   7   6   5   4  11  10   9   8  15  14  13  12
0    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1    1   0   3   2   5   4   7   6   9   8  11  10  13  12  15  14
6    6   7   4   5   2   3   0   1  14  15  12  13  10  11   8   9
7    7   6   5   4   3   2   1   0  15  14  13  12  11  10   9   8
4    4   5   6   7   0   1   2   3  12  13  14  15   8   9  10  11
5    5   4   7   6   1   0   3   2  13  12  15  14   9   8  11  10
10  10  11   8   9  14  15  12  13   2   3   0   1   6   7   4   5
11  11  10   9   8  15  14  13  12   3   2   1   0   7   6   5   4
8    8   9  10  11  12  13  14  15   0   1   2   3   4   5   6   7
9    9   8  11  10  13  12  15  14   1   0   3   2   5   4   7   6
14  14  15  12  13  10  11   8   9   6   7   4   5   2   3   0   1
15  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
12  12  13  14  15   8   9  10  11   4   5   6   7   0   1   2   3
13  13  12  15  14   9   8  11  10   5   4   7   6   1   0   3   2
like image 160
Quang Hoang Avatar answered Dec 20 '25 19:12

Quang Hoang


Another options to set name of that axis to None.

lookups.reindex(lookups[2]).rename_axis(None)

Output:

     0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
2    2   3   0   1   6   7   4   5  10  11   8   9  14  15  12  13
3    3   2   1   0   7   6   5   4  11  10   9   8  15  14  13  12
0    0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
1    1   0   3   2   5   4   7   6   9   8  11  10  13  12  15  14
6    6   7   4   5   2   3   0   1  14  15  12  13  10  11   8   9
7    7   6   5   4   3   2   1   0  15  14  13  12  11  10   9   8
4    4   5   6   7   0   1   2   3  12  13  14  15   8   9  10  11
5    5   4   7   6   1   0   3   2  13  12  15  14   9   8  11  10
10  10  11   8   9  14  15  12  13   2   3   0   1   6   7   4   5
11  11  10   9   8  15  14  13  12   3   2   1   0   7   6   5   4
8    8   9  10  11  12  13  14  15   0   1   2   3   4   5   6   7
9    9   8  11  10  13  12  15  14   1   0   3   2   5   4   7   6
14  14  15  12  13  10  11   8   9   6   7   4   5   2   3   0   1
15  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
12  12  13  14  15   8   9  10  11   4   5   6   7   0   1   2   3
13  13  12  15  14   9   8  11  10   5   4   7   6   1   0   3   2
like image 43
Scott Boston Avatar answered Dec 20 '25 21:12

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!