Skip to content

Instantly share code, notes, and snippets.

View tuples_of_values_indexing.py
## Access element by indexing
new_column1 = [i[0] for i in df.Nums]
new_column2 = [i[1] for i in df.Nums]
Out[36]:
['976', '984', '246', '252', '1419', '103', '85', '85',
'244', '540', '252', '250', '248', '248', '188', '190', '120']
Out[38]:
['660', '360', '127', '345', '1373', '515', '290', '290',
View series_of_tuples.py
## Modify the food table
food_dict = {"Food": dairy_table['Food'],
"Nums": list(zip(dairy_table['Grams'], dairy_table['Calories']))}
df = pd.DataFrame(food_dict)
## "Column Nums as tuples of values":
Food Nums
index0 Cows' milk, whole (976, 660)
index1 skim, instant milk powder (984, 360)
index2 Buttermilk, cultured (246, 127)
View unique_values.py
## Caveat: (2) columns with unique values
dairy_table['Food'].memory_usage(index=False, deep=True)
Out[120]: 1605
## Convert to category,
dairy_table['Food'].astype('category').memory_usage(index=False, deep=True)
Out[121]: 2262
View new_value_correct.py
## Correct way to set new category values ##
dairy_table['Food_Category'] = dairy_table['Food_Category'].cat.add_categories(['Another_Category'])
dairy_table['Food_Category'].iloc[2] = 'Another_Category'
Out[116]:
index0 Milk
index1 others
index2 Another_Category
index3 Milk
index4 Milk
View new_value.py
## Caveat: (1) less flexible regarding assigning new values
dairy_table['Food_Category'] = dairy_table['Food_Category'].astype('category')
dairy_table['Food_Category'].iloc[2] = 'Another_Category'
In [112]: ValueError: Cannot setitem on a Categorical with a new category, set the categories first
View category_backend.py
## Convert Food_Category to the category-dtype and check backend codes ##
dairy_table['Food_Category'].astype('category').cat.codes
Out[97]:
index0 1
index1 3
index2 1
index3 1
index4 1
......
View where.py
## where() to replace values
food_names_list = ["Cows' milk, whole", 'skim', 'Buttermilk, cultured']
boolean_idx = dairy_table["Food"].isin(food_names_list)
dairy_table['Food'].where(boolean_idx, other='Others')
Out[187]:
index0 Cows' milk, whole
index1 skim
index2 Buttermilk, cultured
index3 Others
View mapping.py
## Reverse the mapping to match with the food values
map_dict_reverse = {val:key for key,vals in map_dict.items() for val in vals}
##==== Mapping trick: func Map() to work with Pandas SERIES; fillna = 'others'
dairy_table['Food_Category'] = dairy_table['Food'].map(map_dict_reverse).fillna('others')
Out[134]:
index0 Milk
index1 others
index2 Milk
View dict.py
## Define a dictionary:
map_dict = {'Milk': ("Cows' milk, whole",'skim','Buttermilk, cultured','Evaporated, undiluted',
'Fortified milk','Powdered milk, whole','skim, instant','skim, non-instant',
"Goats' milk, fresh",'Malted milk (1/2 cup ice cream)',
"Ice milk, commercial"),
'Pudding': ("Milk pudding (cornstarch)", "Custard, baked"),
'Cream': ("Ice cream, commercial", "Cream, light, or half-and-half")}
View food_groups.py
## Grouping the Food values
map_dict = {'Milk': ("Cows' milk, whole",'skim','Buttermilk, cultured','Evaporated, undiluted',
'Fortified milk','Powdered milk, whole','skim, instant','skim, non-instant',
"Goats' milk, fresh",'Malted milk (1/2 cup ice cream)',
"Ice milk, commercial"),
'Pudding': ("Milk pudding (cornstarch)", "Custard, baked"),
'Cream': ("Ice cream, commercial", "Cream, light, or half-and-half")}