My dataframe is like
a = {'A': {0: 40.1, 1: 40.1, 2: 40.1, 3: 45.45, 4: 41.6, 5: 39.6},
'B': {0: 41.0, 1: 43.6, 2: 41.65, 3: 47.7, 4: 46.0, 5: 42.95},
'C': {0: 826.0, 1: 835.0, 2: 815.0, 3: 169.5, 4: 170.0, 5: 165.5},
'D': {0: 889.0, 1: 837.0, 2: 863.3, 3: 178.8, 4: 172.9, 5: 170.0}}
a = pd.DataFrame(a)
#a
A B C D
0 40.10 41.00 826.0 889.0
1 40.10 43.60 835.0 837.0
2 40.10 41.65 815.0 863.3
3 45.45 47.70 169.5 178.8
4 41.60 46.00 170.0 172.9
5 39.60 42.95 165.5 170.0
I want to divide columns C and D by 5 but only upto 2nd index
With the help of this I came up with
a.apply(lambda x: x/5 if 'C' in x.name or 'D' in x.name else x)
As you have thought, it applies on the whole column.
Any way to apply it only upto the 2nd index and keep them inplace