Home > Mobile >  Converting cells in a dataframe to binary values if they contain a certain string
Converting cells in a dataframe to binary values if they contain a certain string

Time:07-13

I have a dataframe with a column called player_traits, which contains zero, one or more traits for each player, e.g. 'Injury Prone', 'Long Shot Taker', 'Power Header' etc.

I want to change this column so that if a cell contains 'Injury Prone' its value is converted to 1, and if the cell does not contain 'Injury Prone' its value is converted to 0

A screenshot of my dataframe, with player_traits on the rightmost column

Any tips much appreciated

CodePudding user response:

import pandas as pd
x = pd.DataFrame({"player_traits": ['Injury Prone', 'Long Shot Taker', 'Power Header']})

The data:

      player_traits
 0     Injury Prone
 1  Long Shot Taker
 2     Power Header

We can use the following code:

x["player_traits"] = (x["player_traits"] == "Injury Prone").astype(int)

Which gives:

     player_traits
0                1
1                0
2                0

CodePudding user response:

This will cycle through all of your different traits and assign a binary value for them:

###### Recreate OP's data########
import pandas as pd
traits = ['Injury Prone', 'Long Shot Taker', 'Power Header']
players = ["Player1", "Player2"]
player_traits = ['Injury Prone, Long Shot Taker', 'Power Header']
df = pd.DataFrame({"Player":players, "Player_Traits":player_traits})
#################################

pTraits = []
for x in df.Player_Traits:
    binary = "".join(["1" if y in x else "0" for y in traits])
    pTraits.append(binary)
df["Binary"] = pTraits
df

Ouput:


    Player  Player_Traits                   Binary
0   Player1 Injury Prone, Long Shot Taker   110
1   Player2 Power Header                    001

You can also change df["Binary"] = pTraits to df["Player_Traits"] = pTraits to replace all of the values in the Player_Traits column with their respective Binary... Ouput:

    Player  Player_Traits
0   Player1          110
1   Player2          001
  • Related