您好,登錄后才能下訂單哦!
以下代碼是基于python3.5.0編寫的
import pandas food_info = pandas.read_csv("food_info.csv") # ---------------------特定列加減乘除------------------------- print(food_info["Iron_(mg)"]) 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 # ---------------------某兩列相乘--------------------------- water_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"] # ----------------------把某一列除1000,再添加新列---------------------------- 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)"] * 2 weighted_fat = -0.75 * food_info["Lipid_Tot_(g)"] initial_rating = weighted_protein + weighted_fat #----------------------------數據歸一化----------------------------------- max_calories = food_info["Energ_Kcal"].max() #找列最大值 normalized_calories = food_info["Energ_Kcal"] / max_calories normalized_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_protein food_info["Normalized_Fat"] = normalized_fat # -------------------------------排序---------------------------------- food_info.sort_values("Sodium_(mg)", inplace=True) #升序,inplace=True表示不從建DataFrame print(food_info["Sodium_(mg)"]) food_info.sort_values("Sodium_(mg)", inplace=True, ascending=False) #降序,ascending=False表示降序 print(food_info["Sodium_(mg)"])
以上這篇pandas數值計算與排序方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。