'How can I apply multiple conditions in Pandas, with Python?

How can I apply multiple conditions in pandas? For example I have this dataframe

Country   VAT        
RO       RO1449488
RO       RO1449489
RO       RO1449486
MD       2980409450027

For example I want for the Countries with RO, to delete the "RO" from VAT and remain just number. Or for example if the Country is not RO and let(VAT) is 13, to add "03" in front of VAT

Output to be like

Country   VAT        
RO       1449488
RO       1449489
RO       1449486
MD       032980409450027

I know how to do this with openpyxl, but pandas is new for me and I find pandas syntax to be harder to understand.



Solution 1:[1]

You can use boolean selection with numpy.select:

import numpy as np
                       # condition1            condition2
df['VAT'] = np.select([df['Country'].eq('RO'), df['VAT'].str.len().eq(13)],
                       # replacement1      
                      [df['VAT'].str.replace('^RO', '', regex=True),
                      '03'+df['VAT']], # replacement2
                       df['VAT']       # default
                     )

output:

  Country              VAT
0      RO          1449488
1      RO          1449489
2      RO          1449486
3      MD  032980409450027

Solution 2:[2]

If you want to have something else than @mozway one-liner, you can use apply() to make any modification over your DataFrame. Defining your code in a function :

def myFunc(x):
    if x['VAT'].startswith("RO"):
        result = x['VAT'][2:]
    elif x['Country'] != 'RO' and len(x['VAT']) == 13:
        result = "03" + x['VAT']
    # add other conditions here

    return result

Then you can apply it to your DataFrame row by row with axis=1

df['VAT'] = df.apply(myFunc, axis=1)
# Output
  Country              VAT
0      RO          1449488
1      RO          1449489
2      RO          1449486
3      MD  032980409450027

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway
Solution 2