How to sort the DataFrame table using the "sort_values method".
There is also a sort_index method, but it is not recommended. FutureWarning: by argument to sort_index is deprecated, please use .sort_values(by=...)
** ▼ Basic syntax **
df.sort_values(by=['A'])
└ "df": Table data
└ "by = []": Column / row that is the basis for sorting * Required *
└ "A": Column / row name
option | Contents |
---|---|
axis=0 | The axis is a column. Default. Optional |
axis=1 | Make the axis a row. |
by=['A'] | Axis line/Column name. Mandatory |
assending=True | Ascending sort. Default. Optional |
assending=False | Descending sort. |
na_position='first' | Move NaN to the top. |
na_position='last' | Move NaN to the bottom. Default. Optional |
inplace=False | Do not overwrite. Default. Optional |
inplace=True | Overwrite. |
Creating a table
import pandas as pd
import numpy as np
row0 = [0, 0, 'a', 'Ah', 'one']
row1 = [2, 2, 'c', 'U', 'three']
row2 = [4, np.nan,'e', 'O', 'Samurai']
row3 = [1, 1,'b', 'I', 'Two']
row4 = [3, 3,'d', 'e', '肆']
row5 = [5, 5,'e', 'O', 'Samurai']
df = pd.DataFrame([row0,row1,row2,row3,row4,row5])
df.columns = ['col0', 'col1', 'col2' ,'col3', 'col4']
df.index = ['row0', 'row1', 'row2', 'row3', 'row4', 'row5']
df
Import numpy and use "np.nan" to use the missing value NaN.
Enter the column name that will be the sort standard in "A".
The default is "ascending = True" (optional).
Ascending sort
df.sort_values(by=['col0'])
Sorted in ascending order by the element in column col0.
You can sort alphabets and hiragana in the same way.
Sort in descending order by column name "A".
Descending sort
df.sort_values(by=['col0'], ascending=False)
・ Na_position ='last'
└ Default (optional)
└ Move to the bottom
・ Na_position ='first'
└ Move to the top
Sort columns containing NaN (default)
df.sort_values(by=['col1'])
Sort columns containing NaN (top)
df.sort_values(by=['col1'], na_position='first')
・ ʻInplace = False` └ Default (optional) └ Do not overwrite
・ ʻInplace = True` └ Overwrite
Do not overwrite
df.sort_values(by=['col1'])
df
Overwrite
df.sort_values(by=['col1'], inplace=True)
df
The row direction can only be a representation with numbers or cannot be sorted.
Table to use
import pandas as pd
col0 = [10, 9, 8, 7]
col1 = [1, 10, 100, 1000]
col2 = [2, 2, 2, 2]
col3 = [0.3, 0.03, 0.003, 0.0003]
col4 = [4, 40, 400, 4000]
df2 = pd.DataFrame(col0, columns=['col0'])
df2['col1'] = col1
df2['col2'] = col2
df2['col3'] = col3
df2['col4'] = col4
df2
Ascending sort
df2.sort_values(by=[1], axis=1)
Descending sort
df2.sort_values(by=[1], axis=1, ascending=False)
Recommended Posts