I have a pandas dataframe as below:
+------+---+
|Name |age|
+------+---+
|Mona |12 |
+------+---+
|Monika|25 |
+------+---+
|Tomas |3 |
+------+---+
|Ilyas |47 |
+------+---+
Now, I want to assign some values in a new column to the Name
column contains some substring. For example if it contains Mon then we assign text
to them and if it contains as
we assign city
to them.
So the output must be:
+------+---+----+
|Name |age|new |
+------+---+----+
|Mona |12 |text|
+------+---+----+
|Monika|25 |text|
+------+---+----+
|Tomas |3 |city|
+------+---+----|
|Ilyas |47 |city|
+------+---+----+
I wrote the following codes and it didn't work:
df['new'] = np.nan
df['new'] = df['new'].mask( 'Mon' in df['Name'], 'text')
df['new'] = df['new'].mask( 'as' in df['Name'], 'city')