pandas
Transforming a Column
We can use the arithmetic operators to transform a numerical column.
div_1000 = food_info["Iron_(mg)"] / 1000
add_100 = food_info["Iron_(mg)"] + 100
sub_100 = food_info["Iron_(mg)"] - 100
mult_2 = food_info["Iron_(mg)"]*2
sodium_grams = food_info["Sodium_(mg)"] / 1000
sugar_milligrams = food_info["Sugar_Tot_(g)"] * 1000
Performing Math with Multiple Columns
we can transform columns by other columns. When we use an arithmetic operator between two columns (Series objects), pandas will perform that computation in a pair-wise fashion, and return a new Series object.
water_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"]
grams_of_protein_per_gram_of_water = food_info["Protein_(g)"] / food_info["Water_(g)"]
milligrams_of_calcium_and_iron = food_info["Calcium_(mg)"] + food_info["Iron_(mg)"]
Normalizing Columns in a Data Set
While there are many ways to normalize data, one of the simplest ways is called rescaling.
To calculate a column's minimum value, we use the Series.min() method. We can use the equivalent Series.max() method to compute the maximum value.
x' = \frac{x - min(x)} {max(x) - min(x)}
x represents a column and x′ is the new rescaled column.
Instructions
- Normalize the values in the "Protein_(g)" column, and assign the result to normalized_protein.
- Normalize the values in the "Lipid_Tot_(g)" column, and assign the result to normalized_fat.
print(food_info["Protein_(g)"][0:5])
max_protein = food_info["Protein_(g)"].max()
normalized_protein = (food_info["Protein_(g)"] - food_info["Protein_(g)"].min()) / (food_info["Protein_(g)"].max() - food_info["Protein_(g)"].min())
normalized_fat = (food_info["Lipid_Tot_(g)"] - food_info["Lipid_Tot_(g)"].min()) / (food_info["Lipid_Tot_(g)"].max() - food_info["Lipid_Tot_(g)"].min())
Creating a New Column
So far, we've assigned the Series object that results from a column transform to a variable. However, we can add it to the DataFrame as a new column instead.
We add bracket notation to specify the name we want for that column, then use the assignment operator (=) to specify the Series object containing the values we want to assign to that column:
iron_grams = food_info["Iron_(mg)"] / 1000
food_info["Iron_(g)"] = iron_grams
The DataFrame food_info now includes the "Iron_(g)" column, which contains the values from iron_grams.
Instructions
- Assign the normalized "Protein_(g)" column to a new column named "Normalized_Protein" in food_info.
- Assign the normalized "Lipid_Tot_(g)" column to a new column named "Normalized_Fat" in food_info.
normalized_protein = (food_info["Protein_(g)"] - food_info["Protein_(g)"].min()) /(food_info["Protein_(g)"].min()- food_info["Protein_(g)"].max())
normalized_fat = (food_info["Lipid_Tot_(g)"] - food_info["Lipid_Tot_(g)"].min()) / (food_info["Lipid_Tot_(g)"].max() - food_info["Lipid_Tot_(g)"].min())
food_info["Normalized_Protein"] = normalized_protein
food_info["Normalized_Fat"] = normalized_fat
Sorting a DataFrame by a Column
DataFrame objects have a sort_values() method that we can use to sort the entire DataFrame.
To sort the DataFrame on the Sodium_(mg) column, pass in the column name to the DataFrame.sort_values() method, and assign the resulting DataFrame to a new variable:
food_info.sort_values("Sodium_(mg)")
By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame, rather than modifying food_info itself.
# Sorts the DataFrame in-place, rather than returning a new DataFrame.
food_info.sort_values("Sodium_(mg)", inplace=True)
# Sorts by descending order, rather than ascending.
food_info.sort_values("Sodium_(mg)", inplace=True, ascending=False)
Instructions
- Sort the food_info DataFrame in-place on the Norm_Nutr_Index column in descending order.
food_info["Norm_Nutr_Index"] = 2*food_info["Normalized_Protein"] + (-0.75*food_info["Normalized_Fat"])
food_info.sort_values("Norm_Nutr_Index", inplace=True, ascending=False)