Create new column in pandas based on value of another column
<p>I have some dataset about genders of various individuals. Say, the dataset looks like this:</p>

<pre><code>Male
Female
Male and Female
Male
Male
Female
Trans
Unknown
Male and Female
</code></pre>

<p>Some identify themselves as Male, some female and some identify themselves as both male and female.</p>

<p>Now, what I want to do is create a new column in Pandas which maps </p>

<pre><code>Males to 1, 
Females to 2,
Others to 3
</code></pre>

<p>I wrote some code </p>

<pre><code>def gender(x):
    if x.str.contains("Male")
        return 1
    elif x.str.contains("Female")
        return 2
    elif return 3

df["Gender Values"] = df["Gender"].apply(gender)
</code></pre>

<p>But I was getting errors that function doesn't contain any attribute contains. I tried removing str:</p>

<pre><code>x.contains("Male")
</code></pre>

<p>and I was getting same error</p>

<p>Is there a better way to do this?</p>

<p>You can use:</p>

<pre><code>def gender(x):
    if "Female" in x and "Male" in x:
        return 3
    elif "Male" in x:
        return 1
    elif "Female" in x:
        return 2
    else: return 4

df["Gender Values"] = df["Gender"].apply(gender)

print (df)
            Gender  Gender Values
0             Male              1
1           Female              2
2  Male and Female              3
3             Male              1
4             Male              1
5           Female              2
6            Trans              4
7          Unknown              4
8  Male and Female              3
</code></pre>

python pandas