Reputation: 89
I am trying to sort the columns of a panda on every row. Consider this dataframe df:
X C1 C2 C3 C4 Y Z
A 11 15 12 13 A1 A2
B 21 25 22 23 B1 B2
C 31 35 32 33 C1 C2
I am trying to sort columns in ascending order C1-C4 on every row such that the end dataframe looks like this:
X C1 C2 C3 C4 Y Z
A 11 12 13 15 A1 A2
B 21 22 23 25 B1 B2
C 31 32 33 35 C1 C2
I have looked up pandas sort_values()
but having difficulty with the manual. Hoping somebody can show me some tricks. I am trying to sort only a few columns and not all on each row.
Upvotes: 2
Views: 84
Reputation: 862521
Use numpy.sort
for sorting by all rows:
cols = ['C1','C2','C3','C4']
df[cols] = np.sort(df[cols], axis=1)
print (df)
X C1 C2 C3 C4 Y Z
0 A 11 12 13 15 A1 A2
1 B 21 22 23 25 B1 B2
2 C 31 32 33 35 C1 C2
If possible, sort by index 0
:
cols = ['C1','C2','C3','C4']
df[cols] = df[cols].sort_values(0, axis=1)
#thanks @pygo for another solution
df[cols].sort_values(0, axis=1, inplace=True)
print (df)
X C1 C2 C3 C4 Y Z
0 A 11 12 13 15 A1 A2
1 B 21 22 23 25 B1 B2
2 C 31 32 33 35 C1 C2
Difference between solutions with changed input DataFrame:
print (df)
X C1 C2 C3 C4 Y Z
0 A 11 15 12 13 A1 A2
1 B 2 1 5 4 B1 B2
2 C 31 35 32 33 C1 C2
cols = ['C1','C2','C3','C4']
df[cols] = np.sort(df[cols], axis=1)
print (df)
X C1 C2 C3 C4 Y Z
0 A 11 12 13 15 A1 A2
1 B 1 2 4 5 B1 B2
2 C 31 32 33 35 C1 C2
cols = ['C1','C2','C3','C4']
df[cols] = df[cols].sort_values(0, axis=1)
print (df)
X C1 C2 C3 C4 Y Z
0 A 11 12 13 15 A1 A2
1 B 2 5 4 1 B1 B2
2 C 31 32 33 35 C1 C2
Upvotes: 3
Reputation: 34046
You can use pandas
function sort_values()
like below:
In [331]: df
Out[331]:
X C1 C2 C3 C4 Y Z
0 A 11 15 12 13 A1 A2
1 B 21 25 22 23 B1 B2
2 C 31 35 32 33 C1 C2
In [332]: df.sort_values(['C1','C2','C3','C4'])
Out[332]:
X C1 C2 C3 C4 Y Z
0 A 11 15 12 13 A1 A2
1 B 21 25 22 23 B1 B2
2 C 31 35 32 33 C1 C2
Upvotes: 2