Shivam
Shivam

Reputation: 243

How to sort the values in dataframe?

I am trying to sort the values but not getting the desirable result. Can you please help me how to do this?

Example:

df = pd.read_csv("D:/Users/SPate233/Downloads/iMedical/sqoop/New folder/metadata_file_imedical.txt", delimiter='~')
#df.sort_values(by = ['dependency'], inplace = True)
df.sort_values('dependency', ascending=True, inplace=True)
print(list(df['dependency'].unique()))

Output:

['0', '1', '1,10,11,26,28,55', '1,26,28', '10', '11', '12', '17,42', '2', '26,28', '33', '42', '6']

Desirable_output:

['0', '1', '2', '6', '10', '11', '12', '33', '42', '17,42', '26,28', '1,26,28', '1,10,11,26,28,55']

Upvotes: 0

Views: 52

Answers (1)

Roy2012
Roy2012

Reputation: 12493

Order by the length of the string, and then by its value:

df.assign(len = df.dependency.str.len()).sort_values(["len", "dependency"])

The output is (leaving the len column in for clarity):

          dependency  len
0                  0    1
1                  1    1
8                  2    1
12                 6    1
4                 10    2
5                 11    2
6                 12    2
10                33    2
11                42    2
7              17,42    5
9              26,28    5
3            1,26,28    7
2   1,10,11,26,28,55   16

Upvotes: 2

Related Questions