0

I need help with this code:

d={'Name': ['Mark', 'Lala', "Nina", 'Catherine', 'Izzy', 'Ozno', 'Kim'],
    'Level' : ['A', 'B', 'C', 'D', 'E', 'D', 'D'], 
    'Seats' : [3000, 5000, 4000, 1000, 1000, 2600, 2400]}

df = pd.DataFrame(data = d)

I want to add a new column called "Level_corrected", this is a duplicate of df['Level'], but if df['Level'] = 'D' and df['Seats'] <2500, than the 'D' value in df['Level_corrected'] will become 'D-'.

The desired result is:

d={'Name': ['Mark', 'Lala', "Nina", 'Catherine', 'Izzy', 'Ozno', 'Kim'],
            'Level' : ['A', 'B', 'C', 'D', 'E', 'D', 'D'], 
            'Seats' : [3000, 5000, 4000, 1000, 1000, 2600, 2400],
            'Level_corrected': ['A', 'B', 'C', 'D-', 'E', 'D', 'D-']}

df = pd.DataFrame(data = d)

I've done several attempts (I didn't save the code ...), but it seems like the error is because of the different data types. The Level column is an 'object' and the Seats column is a float64.

Could someone please help me?

Many thanks!

chinneywow
  • 11
  • 2

1 Answers1

3

Use Series.mask with chained both masks with & for bitwise AND and compare by Series.eq for equal and Series.lt for less:

df['Level_corrected'] = df['Level'].mask(df['Level'].eq('D') & df['Seats'].lt(2500),  'D-')
print (df)
        Name Level  Seats Level_corrected
0       Mark     A   3000               A
1       Lala     B   5000               B
2       Nina     C   4000               C
3  Catherine     D   1000              D-
4       Izzy     E   1000               E
5       Ozno     D   2600               D
6        Kim     D   2400              D-
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252