繁体   English   中英

pandas dataframe 中两列之间的相关性不起作用?

[英]correlation between two columns in pandas dataframe not working?

我按照本指南进行关联:

使用.corr 获取两列之间的相关性

这是我的代码:

import pandas as pd

df = pd.DataFrame({'a':[1, 1, 1, 1], 'b':[1, 1, 1, 1]})
print(df)
cor = df['a'].corr(df['b'])

print('the correlation:')
print(cor)

这是结果(数据框两列 1):

   a  b
0  1  1
1  1  1
2  1  1
3  1  1
the correlation:
nan

我期望 correlation 为 1,但它是nan 为什么会这样,我该如何纠正?

似乎这里的相关性为零,这适用于一组不同的数字。

所以这将返回一个数字:

import pandas as pd

df = pd.DataFrame({'a':[1, 2, 3, 4], 'b':[1, 2, 3, 4]})
print(df)
cor = df['a'].corr(df['b'])

print('the correlation:')
print(cor)

返回 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM