Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort_values based on column index

I have seen lots of advice about sorting based on a pandas column name but I am trying to sort based on the column index.

I have included some code to demonstrate what I am trying to do.

import pandas as pd

df = pd.DataFrame({
 'col1' : ['A', 'A', 'B', 'D', 'C', 'D'],
 'col2' : [2, 1, 9, 8, 7, 4],
 'col3': [0, 1, 9, 4, 2, 3],
 })

df2 = df.sort_values(by=['col2'])

I want to sort a number of dataframes that all have different names for the second column. It is not practical to sort based on (by=['col2'] but I always want to sort on the second column (i.e. Column index 1). Is this possible?

like image 570
Mark D Avatar asked Oct 10 '18 10:10

Mark D


People also ask

How do I select a column based on an index?

If you'd like to select columns based on integer indexing, you can use the . iloc function. If you'd like to select columns based on label indexing, you can use the . loc function.

How do you sort DataFrame based on column values?

To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order. It does not modify the original DataFrame.

How do you sort a DataFrame based on an index?

To sort a Pandas DataFrame by index, you can use DataFrame. sort_index() method. To specify whether the method has to sort the DataFrame in ascending or descending order of index, you can set the named boolean argument ascending to True or False respectively. When the index is sorted, respective rows are rearranged.

How do you select rows based on index?

You can select rows from a list index using index. isin() Method which is used to check each element in the DataFrame is contained in values or not.


1 Answers

Select columns name by position and pass to by parameter:

print (df.columns[1])
col2

df2 = df.sort_values(by=df.columns[1])
print (df2)
  col1  col2  col3
1    A     1     1
0    A     2     0
5    D     4     3
4    C     7     2
3    D     8     4
2    B     9     9
like image 101
jezrael Avatar answered Oct 23 '22 11:10

jezrael