Python-Pandas(2)数值计算与排序

来源:互联网 发布:2017php应聘要求 编辑:程序博客网 时间:2024/06/05 09:24
import pandasfood_info = pandas.read_csv("food_info.csv")col_names = food_info.columns.tolist()print(col_names)print(food_info.head(3))

这里写图片描述

#print food_info["Iron_(mg)"]#div_1000 = food_info["Iron_(mg)"] / 1000#print div_1000# Adds 100 to each value in the column and returns a Series object.#add_100 = food_info["Iron_(mg)"] + 100# Subtracts 100 from each value in the column and returns a Series object.#sub_100 = food_info["Iron_(mg)"] - 100# Multiplies each value in the column by 2 and returns a Series object.#mult_2 = food_info["Iron_(mg)"]*2
#It applies the arithmetic operator to the first value in both columns, the second value in both columns, and so onwater_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"]water_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"]iron_grams = food_info["Iron_(mg)"] / 1000  food_info["Iron_(g)"] = iron_grams
#Score=2×(Protein_(g))−0.75×(Lipid_Tot_(g))weighted_protein = food_info["Protein_(g)"] * 2weighted_fat = -0.75 * food_info["Lipid_Tot_(g)"]initial_rating = weighted_protein + weighted_fat
# the "Vit_A_IU" column ranges from 0 to 100000, while the "Fiber_TD_(g)" column ranges from 0 to 79#For certain calculations, columns like "Vit_A_IU" can have a greater effect on the result, #due to the scale of the values# The largest value in the "Energ_Kcal" column.max_calories = food_info["Energ_Kcal"].max()# Divide the values in "Energ_Kcal" by the largest value.normalized_calories = food_info["Energ_Kcal"] / max_caloriesnormalized_protein = food_info["Protein_(g)"] / food_info["Protein_(g)"].max()normalized_fat = food_info["Lipid_Tot_(g)"] / food_info["Lipid_Tot_(g)"].max()food_info["Normalized_Protein"] = normalized_proteinfood_info["Normalized_Fat"] = normalized_fat
#By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame# Sorts the DataFrame in-place, rather than returning a new DataFrame.#print food_info["Sodium_(mg)"]food_info.sort_values("Sodium_(mg)", inplace=True)print food_info["Sodium_(mg)"]#Sorts by descending order, rather than ascending.food_info.sort_values("Sodium_(mg)", inplace=True, ascending=False)print food_info["Sodium_(mg)"]

这里写图片描述