OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

rearrange columns in dataframe depending on sorting output

  • Thread starter Thread starter Lucas Aimaretto
  • Start date Start date
L

Lucas Aimaretto

Guest
I have the following data frame:

Code:
df = pd.DataFrame(
    {
        'a':[1,2,3,4,5,6],
        'b':[1,1,3,3,5,5],
        'c':[1,2,3,4,5,6],                
        'd':[1,1,1,1,1,5],
    }
)

In [1051]: df
Out[1051]: 
   a  b  c  d
0  1  1  1  1
1  2  1  2  1
2  3  3  3  1
3  4  3  4  1
4  5  5  5  1
5  6  5  6  5

If I sort the df using all the columns, I get the following:

Code:
In [1055]: columns = list(df.columns)
      ...: 
      ...: dfSorted = df.sort_values(by=columns, ascending=False)
      ...: 
      ...: print(dfSorted)
   a  b  c  d
5  6  5  6  5
4  5  5  5  1
3  4  3  4  1
2  3  3  3  1
1  2  1  2  1
0  1  1  1  1

I'd like to re arrange the order of the columns going from the column with the least differences among the rows, being the last column the one with the most differences. In my example, the expected order should be then d,b,c,a.

This is so because column d has only two different values (1 and 5) while columns c and a have all of the values different. Column b is the middle situation ...

Code:
In [1056]: dfSorted[['d','b','c','a']]
Out[1056]: 
   d  b  c  a
5  5  5  6  6
4  1  5  5  5
3  1  3  4  4
2  1  3  3  3
1  1  1  2  2
0  1  1  1  1

Any idea? Thanks!
<p>I have the following data frame:</p>
<pre><code>df = pd.DataFrame(
{
'a':[1,2,3,4,5,6],
'b':[1,1,3,3,5,5],
'c':[1,2,3,4,5,6],
'd':[1,1,1,1,1,5],
}
)

In [1051]: df
Out[1051]:
a b c d
0 1 1 1 1
1 2 1 2 1
2 3 3 3 1
3 4 3 4 1
4 5 5 5 1
5 6 5 6 5
</code></pre>
<p>If I sort the df using all the columns, I get the following:</p>
<pre><code>In [1055]: columns = list(df.columns)
...:
...: dfSorted = df.sort_values(by=columns, ascending=False)
...:
...: print(dfSorted)
a b c d
5 6 5 6 5
4 5 5 5 1
3 4 3 4 1
2 3 3 3 1
1 2 1 2 1
0 1 1 1 1
</code></pre>
<p>I'd like to re arrange the order of the columns going from the column with the least differences among the rows, being the last column the one with the most differences. In my example, the expected order should be then d,b,c,a.</p>
<p>This is so because column <code>d</code> has only two different values (1 and 5) while columns <code>c</code> and <code>a</code> have all of the values different. Column <code>b</code> is the middle situation ...</p>
<pre><code>In [1056]: dfSorted[['d','b','c','a']]
Out[1056]:
d b c a
5 5 5 6 6
4 1 5 5 5
3 1 3 4 4
2 1 3 3 3
1 1 1 2 2
0 1 1 1 1
</code></pre>
<p>Any idea? Thanks!</p>
 
Top