Osceria
Osceria

Reputation: 353

Update column value using Grouping in Pandas

I'm trying to do a simple automation using Grouping in Pandas for a sample dataframe like below. Appreciate any help.

df = pd.DataFrame({'C1':['A','A','B','B'], 'C2':['A1','A2','B1','B2']})

enter image description here

Expected Output:

enter image description here

Upvotes: 0

Views: 81

Answers (1)

r-beginners
r-beginners

Reputation: 35115

I'm sure there is a more pythonic way to sort the data frames added by the pandas series. I am sure you will get some answers from advanced users with my answers.

a = pd.Series(['A','A'], index=df.columns, name=4)
b = pd.Series(['B','B'], index=df.columns, name=5)
df = df.append(a)
df = df.append(b)
df.sort_values('C2', ascending=True, inplace=True, ignore_index=True)
df
    C1  C2
0   A   A
1   A   A1
2   A   A2
3   B   B
4   B   B1
5   B   B2

Upvotes: 1

Related Questions