pandas总结数据相关周密的实例,pandas数值总括与

作者: 韦德国际1946手机版  发布:2019-05-28

一般来讲所示:

pandas总结数据相关周密的实例,pandas数值总括与排序方法。相见那样二个供给,有一张表,要给那张表新添3个字段delta,delta的值等于每行的c1列的值减去上一行c1列的值。

以下代码是依靠python三.5.0编纂的

正文主要演示pandas中DataFrame对象corr()方法的用法,该措施用来计算DataFrame对象中全体列之间的相关周详(包罗pearson相关周到、Kendall Tau相关周全和spearman秩相关)。

###方法1:用shift函数,不用通过循环

import pandas as pd
import numpy as np
import matplotlib as plt
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
df['diff'] = df['A'] - df['A'].shift(1)

自家的减轻方案,能够通过python的pandas的diff来落到实处,也可以经过sql来落到实处,如下

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)"])
>>> import numpy as np
>>> import pandas as pd

>>> df = pd.DataFrame({'A':np.random.randint(1, 100, 10),
   'B':np.random.randint(1, 100, 10),
   'C':np.random.randint(1, 100, 10)})
>>> df
   A  B  C
0  5 91  3
1 90 15 66
2 93 27  3
3 70 44 66
4 27 14 10
5 35 46 20
6 33 14 69
7 12 41 15
8 28 62 47
9 15 92 77
>>> df.corr() # pearson相关系数
     A       B       C
A 1.000000 -0.560009 0.162105
B -0.560009 1.000000 0.014687
C 0.162105 0.014687 1.000000
>>> df.corr('kendall') # Kendall Tau相关系数

     A       B       C
A 1.000000 -0.314627 0.113666
B -0.314627 1.000000 0.045980
C 0.113666 0.045980 1.000000
>>> df.corr('spearman') # spearman秩相关

     A       B       C
A 1.000000 -0.419455 0.128051
B -0.419455 1.000000 0.067279
C 0.128051 0.067279 1.000000

如上那篇pandas 数据完毕行间总括的形式正是小编分享给我们的全体内容了,希望能给我们3个参阅,也希望大家多多协助脚本之家。

import pandas as pd

srcTable = pd.read_csv('pos1.csv')
print(srcTable)
destTable = srcTable.loc[srcTable.tid == 1, ['ts1', 'ts2']].sort_values(by='ts1')
destTable.columns = ['deltaTs1', 'deltaTs2']
destTable = destTable.diff()
destTable = destTable.fillna(0)
destTable['delay'] = destTable['deltaTs2'] - destTable['deltaTs1']
print(destTable)

上述这篇pandas数值总结与排序方法正是我分享给我们的全部内容了,希望能给我们3个参照,也冀望大家多多支持脚本之家。

上述那篇Python pandas总结数据相关周到的实例正是笔者分享给我们的全体内容了,希望能给大家一个参照,也愿意大家多多帮衬脚本之家。

本文由韦德国际1946发布于韦德国际1946手机版,转载请注明出处:pandas总结数据相关周密的实例,pandas数值总括与

关键词: 韦德娱乐