'How to create keys from lists and get key-value pairs from tuples
I have tuples in a dataframe that look like this:
| items | |
|---|---|
| 0 | (strawberry, ice cream) |
| 1 | (apple, banana, biscuits) |
I have also two separate lists of "fruit" and "snack".
fruit = ["strawberry", "apple", "banana"]
snack = ["ice cream", "biscuits"]
How do I change them to key:value pairs that look like this:
| items | |
|---|---|
| 0 | {"fruit": "strawberry" , "snack": "ice cream"} |
| 1 | {"fruit": ["apple", "banana"] , "snack": "biscuits"} |
Solution 1:[1]
You haven't provided that much information. Based on your sample data :
df1 =pd.DataFrame()
df1['items'] = [('strawberry', 'ice cream'), ('apple', 'banana', 'biscuits')]
A solution would be to make a simple function associating each item with his category.
def findCategory(x):
fruit = ["strawberry", "apple", "banana"]
snack = ["ice cream", "biscuits"]
result = {
'fruit': [],
'snack': []
}
for item in x['items']:
if item in fruit:
result['fruit'].append(item)
elif item in snack:
result['snack'].append(item)
return result
Then you can apply that function on each row of your DataFrame with apply() :
df1['items'] = df1.apply(findCategory, axis=1)
# Output
items
0 {'fruit': ['strawberry'], 'snack': ['ice cream']}
1 {'fruit': ['apple', 'banana'], 'snack': ['bisc...
The findCategory() function created above is working specifically for your example, but can be generalised with more categories, and with dictionnary comprehension if you are confortable with it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Titouan L |
