Home > Mobile >  Convert dataframe to 5*5 grid of choice
Convert dataframe to 5*5 grid of choice

Time:12-09

Following are the standard values for score and grades.

Grade Grade1 Grade2 Grade3 Grade4
B1 A1 IA1 D1 ID1
B2 A2 IA2 D2 ID2
B3 A3 IA3 D3 ID3
B4 A4 IA4 D4 ID4
B5 A5 IA5 D5 ID5

Input :-

Grade Grade1 Grade2 Grade3 Grade4
B5 A1 IA4 D3 ID2

Output:-

Tiers Grade Grade1 Grade2 Grade3 Grade4
1 A1
2 ID2
3 D3
4 IA4
5 B5

CodePudding user response:

Example

data1 = {'Grade': {0: 'B1', 1: 'B2', 2: 'B3', 3: 'B4', 4: 'B5'},
         'Grade1': {0: 'A1', 1: 'A2', 2: 'A3', 3: 'A4', 4: 'A5'},
         'Grade2': {0: 'IA1', 1: 'IA2', 2: 'IA3', 3: 'IA4', 4: 'IA5'},
         'Grade3': {0: 'D1', 1: 'D2', 2: 'D3', 3: 'D4', 4: 'D5'},
         'Grade4': {0: 'ID1', 1: 'ID2', 2: 'ID3', 3: 'ID4', 4: 'ID5'}}
data2 = {'Grade': {0: 'B5'}, 'Grade1': {0: 'A1'}, 'Grade2': {0: 'IA4'},
         'Grade3': {0: 'D3'}, 'Grade4': {0: 'ID2'}}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

Code

make condition by using broadcasting

df1[df1.eq(df2.iloc[0])]

result:

    Grade   Grade1  Grade2  Grade3  Grade4
0   NaN     A1      NaN     NaN     NaN
1   NaN     NaN     NaN     NaN     ID2
2   NaN     NaN     NaN     D3      NaN
3   NaN     NaN     IA4     NaN     NaN
4   B5      NaN     NaN     NaN     NaN
  • Related