Florian Bernard
Florian Bernard

Reputation: 323

Sort a subset of columns for rows matching condition

My DataFrame looks like this:

     a b  c d e f g
   0 x y  1 3 4 5 6
   1 x y -1 7 8 5 6
   2 x y -1 7 8 3 4

For rows where df.c == -1 I would like to sort all the columns between df.d and df.g in ascending order.

The result would be:

     a b  c d e f g
   0 x y  1 3 4 5 6
   1 x y -1 5 6 7 8
   2 x y -1 3 4 7 8

I tried several things but none seemed to work:

for row in df.itertuples():
if row.c == -1:
    subset = row[4:]
    sorted = sorted(subset)
    df.replace(to_replace=subset, value= sorted)

and also

df.loc[df.c == -1, df[4:]] = sorted(df[4:])

Upvotes: 1

Views: 1518

Answers (2)

miradulo
miradulo

Reputation: 29710

You can use numpy.sort on the region of interest.

mask = df.c.eq(-1), slice('d', 'g')

df.loc[mask] = np.sort(df.loc[mask].values)

df
#    a  b  c  d  e  f  g
# 0  x  y  1  3  4  5  6
# 1  x  y -1  5  6  7  8
# 2  x  y -1  3  4  7  8

Upvotes: 3

Peter Leimbigler
Peter Leimbigler

Reputation: 11105

Probably not the fastest, but this works:

rmask = df.c == -1
cmask = ['d', 'e', 'f', 'g']
df.loc[rmask, cmask] = df.loc[rmask, cmask].apply(lambda row: sorted(row), axis=1)
df
   a  b  c  d  e  f  g
0  x  y  1  3  4  5  6
1  x  y -1  5  6  7  8
2  x  y -1  3  4  7  8

Upvotes: 1

Related Questions