Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort a dataframe based on values of another column, using integer data type

I want to sort a pandas dataframe based on a column, but the values are stored as strings, but they should be treated as integers.

df.sort(col1)

where col1 = ['0','1','12','13','3'].

How can I use it so that it considers these numbers as integers and not strings?

like image 646
AMisra Avatar asked Oct 18 '25 15:10

AMisra


1 Answers

If you want to keep your dataframe untouched and just want to sort it...
This is assuming col1 is a column in your dataframe df

option 1

df.iloc[df['col1'].astype(int).argsort()]

option 2
You can also use pd.to_numeric

df.iloc[pd.to_numeric(df['col1']).argsort()]

option 3
For more efficiency you can reconstruct manipulating the underlying numpy array

v = df.values
a = df['col1'].values.astype(int).argsort()
pd.DataFrame(v[a], df.index[a], df.columns)

See also

like image 102
piRSquared Avatar answered Oct 20 '25 15:10

piRSquared