I have this dataframe that consists of 6 columns, and I'm trying to sort on the "Sample Type" column. My dataframe looks like this:
df = pd.DataFrame({'Sample ID': [1,2,3], 'Sample Type':[Metastatis, Metastasis, Primary],
})
I've used the sort_values() function as follows:
df.sort_values(by = ['Sample Type'])
What I expect to see is the dataframe becomes sorted on the Sample Type column, returning the dataframe sorted in alphabetical order. However, what gets returned is that dataframe with alternating Sample Types, which clearly indicates that the sorting that I'm expecting hasn't occurred.
Am I using the function incorrectly? Please point out any errors that I'm overlooking. Thank you
df = pd.DataFrame({'Sample ID': [1,2,3], 'Sample Type':['Metastatis', 'Metastasis', 'Primary'],
})
df
Sample ID Sample Type
0 1 Metastatis
1 2 Metastasis
2 3 Primary
df.sort_values(by='Sample Type')
Sample ID Sample Type
1 2 Metastasis
0 1 Metastatis
2 3 Primary
df.sort_values(by='Sample Type', ascending=False)
Sample ID Sample Type
2 3 Primary
0 1 Metastatis
1 2 Metastasis
Use ascending
AND inplace
also:
df = pd.DataFrame({'Sample ID': [1,2,3], 'Sample Type':['Metastatis', 'Metastasis',
'Primary'],})
df.sort_values(['Sample Type'], ascending=False, inplace=True)
df
Output
Sample ID Sample Type
2 3 Primary
0 1 Metastatis
1 2 Metastasis
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.