Issue
I have a dataset that has 'unknown' in each group. I am thinking to put 'unknown' at the end of each group. For example: I have a code like this:
import pandas as pd
clients1 = {'Name': ['A','B','Unknown','Y','Z','A','B','Unknown','Y','Z'],
'group': ['1','1','1','1','1','2','2','2','2','2']
}
df = pd.DataFrame(clients1, columns= ['Name', 'group'])
df
the output:
Name group
A 1
B 1
Unknown 1
Y 1
Z 1
A 2
B 2
Unknown 2
Y 2
Z 2
the desired output should be:
Name group
A 1
B 1
Y 1
Z 1
Unknown 1
A 2
B 2
Y 2
Z 2
Unknown 2
Does anyone know how to do that?
Solution
You can sort the data by group
and name==Unknown
, so the Unknown
's are marked with True
and put at the bottom:
df = (df.assign(unknown_name=df['Name'].eq('Unknown'))
.sort_values(['group','unknown_name'])
)
Output:
Name group unknown_name
0 A 1 False
1 B 1 False
3 Y 1 False
4 Z 1 False
2 Unknown 1 True
5 A 2 False
6 B 2 False
8 Y 2 False
9 Z 2 False
7 Unknown 2 True
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.