Python Pandas dataframe.corrwith()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas dataframe.corrwith()用于计算两个DataFrame对象的行或列之间的成对相关关系。如果两个数据框架对象的形状不一样,那么相应的相关值将是一个NaN值。
语法: DataFrame.count(axis=0, level=None, numeric_only=False)
参数:
other: 数据框架
axis: 0或’索引’用于计算纵列,1或’列’用于计算横列。
drop :从结果中删除缺失的索引,默认返回所有索引的联合。
返回:相关联:系列
注意:变量与自身的相关度为1。
例子#1:使用corrwith()函数来查找两个数据帧对象之间沿**列axis的相关性。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 7, 8],
"B":[5, 8, 4, 3],
"C":[10, 4, 9, 3]})
# Creating the second dataframe
df2 = pd.DataFrame({"A":[5, 3, 6, 4],
"B":[11, 2, 4, 3],
"C":[4, 3, 8, 5]})
# Print the first dataframe
print(df1, "\n")
# Print the second dataframe
print(df2)
现在,沿着行axis找到两个数据框架的列之间的相关性。
# To find the correlation among the
# columns of df1 and df2 along the column axis
df1.corrwith(df2, axis = 0)
输出 :
输出系列包含了两个数据框架对象的三列之间分别的相关性。
例子#2:使用corrwith()函数来查找两个数据帧对象之间沿**行axis的相关性。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 7, 8],
"B":[5, 8, 4, 3],
"C":[10, 4, 9, 3]})
# Creating the second dataframe
df2 = pd.DataFrame({"A":[5, 3, 6, 4],
"B":[11, 2, 4, 3],
"C":[4, 3, 8, 5]})
# To find the correlation among the
# columns of df1 and df2 along the row axis
df1.corrwith(df2, axis = 1)
输出 :
输出系列包含了两个数据框架对象的四行之间分别的相关性。