Skip to content

Instantly share code, notes, and snippets.

@ocaoimh
Last active April 1, 2023 09:22
Show Gist options
  • Save ocaoimh/c769cc62d7f993343eb7ebb452f7195b to your computer and use it in GitHub Desktop.
Save ocaoimh/c769cc62d7f993343eb7ebb452f7195b to your computer and use it in GitHub Desktop.
Renaming based on condition
#Create a new dataframe with language ids and language names.
#Hindi, Bengali, Marathi, Telugu, Tamil, Gujarati, Urdu
#Corresponding to these language ids: 171, 330, 345, 49, 206, 260, 161
conditions = [
(data2['LanguageID'] == 171),
(data2['LanguageID'] == 330),
(data2['LanguageID'] == 345) ,
(data2['LanguageID'] == 49),
(data2['LanguageID'] == 206),
(data2['LanguageID'] == 260),
(data2['LanguageID'] == 161)
]
# create a list of the values we want to assign for each condition (language ID)
values = ['Hindi', 'Bengali', 'Marathi', 'Telugu', 'Tamil','Gujarati', 'Urdu']
# create a new column and use np.select to assign values (language name) to it using our lists as arguments
data2['Language'] = np.select(conditions, values)
# display updated DataFrame
data2.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment