#2.1.4 Data Manipulation with pandas.md

1.标准化:列/最大值

While there are many ways to normalize data, one of the simplest ways is to divide all of the values in a column by that column's maximum value. This way, all of the columns will range from 0 to 1. To calculate the maximum value of a column, we use the Series.max()
method.

input
max_protein = food_info["Protein_(g)"].max()
normalized_protein = food_info["Protein_(g)"] / max_protein
print(normalized_protein.head(5))

output

0 0.009624 
1 0.009624 
2 0.003170 
3 0.242301 
4 0.263134 
Name: Protein_(g), dtype: float64

2.列之间的加减

food_info["Normalized_Protein"] = food_info["Protein_(g)"] / food_info["Protein_(g)"].max()
food_info["Normalized_Fat"] = food_info["Lipid_Tot_(g)"] / food_info["Lipid_Tot_(g)"].max()
food_info["Norm_Nutr_Index"] = 2*food_info["Normalized_Protein"]  + (-0.75*food_info["Normalized_Fat"])

3.创建一个新列

food_info["Normalized_Protein"] = normalized_protein
food_info["Normalized_Fat"] = normalized_fat

4.升降序排列文档:Dataframe.sort_values(‘YY’, ascending=True)

food_info.sort_values("Norm_Nutr_Index", inplace=True, ascending=False)
  • inplace=True,不创建新的对象,直接在原始对象上尽心修改;
  • inplace=False,在对原始对象进行修改,而会创建新的对象;
  • ascending:
    Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,789评论 0 23
  • 过来人会说 有个懂你的人挺好 被爱的幸福 不是那些没有辛苦爱过别人的人能体会 被爱,在用尊重,感恩的真心回应后 就...
    那个在看大象洗澡的人阅读 205评论 0 0
  • 我喜欢温柔的人 喜欢朝夕相处默契 喜欢相互尊重的独立 喜欢一言不发又相见甚欢 喜欢突如其来的问候 不做作省略了假意...
    仰望星空yiy阅读 141评论 0 0
  • 文/断剑裂冰 咳咳,怎么突然觉得这个话题有点严肃了?!放松,放松,今天我们只聊游戏,不谈人生。 既然谈到游戏,想到...
    筱断阅读 528评论 4 3
  • 第六课 写作:写北京 上一节课同学们仿写得不亦乐乎,但其中还包含了一些问题。我们这节课除了解决一下每位同学各自在仿...
    田源ty阅读 464评论 0 1