Skip to content
Advertisement

sorting .csv file using pandas

I am using pandas.DataFrame.sort_values to sort my csv.

My csv without sorting looks like
this.
I am trying to sort my csv file by numbers in ATOM_id in ascending order. This is my code snippet df.sort_values(["ATOMS_ID"],axis = 0, ascending = [True],inplace = True). This is what I get.
I am not really sure why my .csv is not get sorted as 1,2,3,… but it is getting 1,10,100… .

Also, I was wondering about how to undo the sorting?

Thanks!!

Advertisement

Answer

Your sorting column might be in string type, not integer. You could check it with:

df.dtypes

It will show you what data type each column has.

Then for convert:

df["ATOMS_ID"] = df["ATOMS_ID"].astype(int)

Then try sorting again see if it works.

7 People found this is helpful
Advertisement