Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jimbrayrcp/a7a3fbec7b88d5cc46f32dd52504ecc1 to your computer and use it in GitHub Desktop.
Save jimbrayrcp/a7a3fbec7b88d5cc46f32dd52504ecc1 to your computer and use it in GitHub Desktop.
pandas library, python 3.9, using the following format : dictionary_comprehension = {NEW_KEY: NEW_VALUE for (INDEX, ROW) in DATA.iterrows()} list_comprehension = [NEW_ITEM for ITEM in LIST]
# pandas library,
# python 3.9,
# using the following format :
# dictionary_comprehension = {NEW_KEY: NEW_VALUE for (INDEX, ROW) in DATA.iterrows()}
# list_comprehension = [NEW_ITEM for ITEM in LIST]
import pandas
new_dict = {
'letter': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
'code': ['Alex', 'Bill', 'Charlie', 'Dale',
'Edward', 'Frank', 'Glynn', 'Harry',
'Ivan', 'Jack', 'Kevin', 'Larry',
'Mike', 'Nate', 'Oscar', 'Paul',
'Quinn', 'Ralph','Steve', 'Tim',
'Ulises', 'Victor', 'Walt', 'Xavier',
'Yusuf', 'Zachary'
],
}
df = pandas.DataFrame(new_dict)
names = {row.letter: row.code for (index, row) in df.iterrows()}
new_word = "JACK"
result = [names[letter] for letter in new_word]
print(result)
# RETURNS: ['Jack', 'Alex', 'Charlie', 'Kevin']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment