Skip to content Skip to sidebar Skip to footer

Sorting Values Between Column Using Pandas

Assume I have this simple dataframe matrix = [(222, 16, 23), (333, 31, 51), (4, 34, 11), ] df = pd.DataFrame(matrix, index=list('abc'), columns=list('

Solution 1:

This is a bit of a strange request - usually the columns contain semantic meaning. You can do it via:

cols = df.columns
df = pd.DataFrame(np.sort(df.values, axis=1), columns=cols, index=df.index)

output:

    x   y    z
a1623222b3151333
c  41134

Solution 2:

You can use np.sort over axis 1 here:

# df#      x   y   z# a  222  16  23# b  333  31  51# c    4  34  11

df = pd.DataFrame(np.sort(df.values, axis=1),
                  columns=df.columns,
                  index=df.index)
    x   y    z
a  16  23  222
b  31  51  333
c   4  11   34

Post a Comment for "Sorting Values Between Column Using Pandas"