Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Create new columns based on other column conditions

I have the following DF:

Fecha xG xGA Home Away Score
2022-05-01 1.53 0.45 América Cruz Azul 0:0
2022-04-24 1.46 0.47 Tigres UANL América 0:2
2022-04-21 1.40 0.43 América León 2:0
2022-04-16 2.44 0.65 Club Tijuana América 1:3

I want to create two new columns named HomeXG and AwayXG where the values are taken from the xG and xGA columns under the condition that if America is the home team the xG becomes HomeXG and if they are away xG is used for AwayXG.

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Fecha xG xGA Home Away Score HomeXG AwayXG
2022-05-01 1.53 0.45 América Cruz Azul 0:0 1.53 0.45
2022-04-24 1.46 0.47 Tigres UANL América 0:2 0.47 1.46
2022-04-21 1.40 0.43 América León 2:0 1.40 0.43
2022-04-16 2.44 0.65 Club Tijuana América 1:3 0.65 2.44

>Solution :

You can use where on a 2D slice of the DataFrame:

df[['HomexG', 'AwayxG']] = df[['xG', 'xGA']].where(df['Home'].eq('América'),
                                                   df[['xGA', 'xG']].values)

NB. the second argument of where must be a numpy array to avoid index alignment!

output:

        Fecha    xG   xGA          Home       Away Score  HomexG  AwayxG
0  2022-05-01  1.53  0.45       América  Cruz Azul   0:0    1.53    0.45
1  2022-04-24  1.46  0.47   Tigres UANL    América   0:2    0.47    1.46
2  2022-04-21  1.40  0.43       América       León   2:0    1.40    0.43
3  2022-04-16  2.44  0.65  Club Tijuana    América   1:3    0.65    2.44
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading