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

how to get value from some condition in python

im trying to get death column or feature using value from kill and condition from player and opponent. The table will looks like this

Player Opponent Kill
dicey OXY 4
OXY dicey 6
Verno dapr 5
dapr Verno 7

and how i turn it into

Player Opponent Kill Death
dicey OXY 4 6
OXY dicey 6 4
Verno dapr 5 7
dapr Verno 7 5

im expecting to get death by using kill column as for the value from condition each player and opponent

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

>Solution :

Following code will do it:

import pandas

data_frame = {'Player': ['dicey', 'OXY', 'Verno', 'dapr'],
    'Opponent': ['OXY', 'dicey', 'dapr', 'Verno'],
    'Kill': [4, 6, 5, 7]}
df = pandas.DataFrame(data_frame)
df['Death'] = df.apply(lambda row: df.loc[(df['Player'] == row['Opponent']) &
                                          (df['Opponent'] == row['Player']), 'Kill'].values[0], axis=1)
print(df)
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