脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pandas数值计算与排序方法

pandas数值计算与排序方法

2021-01-31 00:08数据阿伯 Python

下面小编就为大家分享一篇pandas数值计算与排序方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

以下代码是基于python3.5.0编写的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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数值计算与排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_32005671/article/details/61629873

延伸 · 阅读

精彩推荐