sort values and create new column based on result

vdotup

I have this example

import pandas as pd
import numpy as np

rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(0, 5, size=(5, 3)), columns=list("ABC"))
print(df)

which produce this dataframe

   A  B  C
0  4  4  7
1  1  9  6
2  2  9  1
3  0  5  8
4  3  5  5

i want to sort values from column A and B and C from higher to lower and put result column headers in new column D like this:

   A  B  C  D
0  4  4  7  C, A, B
1  1  9  6  B, C, A
2  2  9  1  B, A, C
3  0  5  8  C, B, A
4  3  5  5  B, C, A

I hope it's clear, thank you

Quang Hoang

You can try:

df['D'] = df.apply(lambda x: ', '.join(x.sort_values(ascending=False).index), axis=1)


   A  B  C        D
0  4  4  7  C, B, A
1  1  9  6  B, C, A
2  2  9  1  B, A, C
3  0  5  8  C, B, A
4  3  5  5  C, B, A

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sort values and create new column based on rows Pandas

How to sort dataframe result based on column values

Create a new column based on the values of the column by ID

Create new column based on values of another column

Create a new column based on values in an existing column

Extract cells where columns contain NA values and create a new column based on the result

Hive query: select a column based on the condition another columns values match some specific values, then create the match result as a new column

PySpark: select a column based on the condition another columns values match some specific values, then create the match result as a new column

Create new column based on values in other columns

Conditionally Create New Column Based on Row Values

Create a new column with values based on conditions

Create new names to column based on lookup values

Create new column based on missing values

Create a new column based on values in an existing colomn

Create a new category based on column values: Pandas

create new spark column based on dictionary values

Pandas groupby on two column and create new column in excel based on result

sort a column based on the result of groupby

Create a new column in Pandas Dataframe based on the 'NaN' values in another column

Pandas: To create a new column based on two column values

Create a new column based on other column names and values

Create a new column based on Grouping of similar values in another column in pandas

Create new column based on values in LIST in other column

How to create new column based on values in array column in Pyspark

Create a new column in a dataframe, based on Groupby and values in a separate column

Pandas create new column based on first unique values of existing column

(Python) Create New Column Based on Values of Existing Column

Create new column based on last 2 digits of values in another column

Create a new column based on other column values - conditional forward fill?

TOP Ranking

HotTag

Archive