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)"]
阅读全文
0 0
- Python-Pandas(2)数值计算与排序
- pandas数值计算与排序
- Pandas数值计算与排序
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事
- Python Pandas 排序
- pandas索引与计算
- [Python Learning]数值计算
- Numpy与Pandas排序
- pandas排序与统计
- python科学计算一:pandas
- python科学计算四:pandas
- Python+pandas计算数据相关系数
- Python.Scipy -- 数值计算库
- python科学计算-数值计算-NumPy
- python 科学计算库 - Numpy,Scipy,Pandas
- python科学计算——pandas
- python科学计算笔记(四)pandas 数据索引与选取
- Python之路:数值排序
- Velocity模板相关学习资料
- 六大设计原则之开闭原则
- 【exceptions】java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to xxxxx
- AC自动机算法简介(洛谷P3808)
- Python学习笔记之错误处理(关键词:错误处理、异常机制、try、except、else、finally、raise)
- Python-Pandas(2)数值计算与排序
- 使用Docker的macvlan为容器提供的桥接网络
- [LeetCode]141. Linked List Cycle
- xml文档解析 DOM和DOM4J分析
- Linux下创建进程的三种方式及特点
- Javaweb容器的四种作用域
- hdu 6102 GCDispower 树状数组+容斥
- OnGUI---EditorWindow中的OnGUI事件
- 学习java中的filter(过滤器)