Assign Categories to dataframe

Input:

df = {'A': ['aa','b',2,3,4,"bb",5,6,7,8],'B': ['aa',5,6,7,8,"bb",1,2,3,4]}

df = pd.DataFrame(df)
    A   B   
0   aa  aa  
1   b   5   
2   2   6   
3   3   7   
4   4   8   
5   bb  bb  
6   5   1   
7   6   2   
8   7   3   

I have a dataframe(df), I Need assign a category column based on the columns [‘A’, ‘B’]

Expected Output:

    A   B       Cat
0   aa  aa      
1   b   5       aa
2   2   6       aa
3   3   7       aa
4   4   8       aa
5   bb  bb      
6   5   1       bb
7   6   2       bb
8   7   3       bb

Answer

You can use a mask on the empty value in C and where/mask/ffill:

mask = df['C'].eq('')
df['Cat'] = df['A'].where(mask).ffill().mask(mask, '')

NB. I used only A to define the name as A/B are identical when C is empty string

output:

    A   B   C Cat
0  aa  aa        
1   1   5   2  aa
2   2   6   3  aa
3   3   7   4  aa
4   4   8   5  aa
5  bb  bb        
6   5   1   7  bb
7   6   2   8  bb
8   7   3   9  bb
9   8   4  10  bb

alternative without column C

I checked here is the type is string for A or B and the value equal in a and B:

df = pd.DataFrame({'A': ['aa','b',2,3,4,"bb",5,6,7,8],'B': ['aa',5,6,7,8,"bb",1,2,3,4]})

mask = df['A'].eq(df['B']) & (df['A'].str.isnumeric().eq(False) | df['B'].str.isnumeric().eq(False))
df['Cat'] = df['A'].where(mask).ffill().mask(mask, '')

output:

    A   B Cat
0  aa  aa    
1   b   5  aa
2   2   6  aa
3   3   7  aa
4   4   8  aa
5  bb  bb    
6   5   1  bb
7   6   2  bb
8   7   3  bb
9   8   4  bb