解决方法
查看dataframe的数据类型,转为数值类型即可:
dataframe = dataframe.astype("float")
或者逐列转换:
for _i, _value in dataframe.iteritems():
dataframe.loc[:, _i] = pd.to_numeric(_value)
问题复现
import pandas as pd
import numpy as np
dataframe = pd.DataFrame(np.random.random(size=(10, 5)))
这时计算.corr()
会得到预期的结果:
但如果转为object类型就会变为空值:
此时的()
得到的结果为:
RangeIndex: 10 entries, 0 to 9
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 10 non-null object
1 1 10 non-null object
2 2 10 non-null object
3 3 10 non-null object
4 4 10 non-null object
dtypes: object(5)
memory usage: 528.0+ bytes