Learn Data Analysis with Python by A. J. Henley & Dave Wolf

Learn Data Analysis with Python by A. J. Henley & Dave Wolf

Author:A. J. Henley & Dave Wolf
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


Method 1: Convert single column to hold numeric variables (Listing 3-47).

def score_to_numeric(x):

if x=='female':

return 1

if x=='male':

return 0

Listing 3-47Function to Convert Gender to Number

Now, run that method on your column (Listing 3-48).

df['gender_val'] = df['gender'].apply(score_to_numeric)

df.tail()

Listing 3-48Apply score_to_numeric Function to Gender

Method 2: Create individual Boolean columns (Listing 3-49).

df_gender = pd.get_dummies(df['gender'])

df_gender.tail()

Listing 3-49Create Boolean Columns Based on Gender Column



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.