日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pandas計(jì)算相關(guān)系數(shù)corr返回空的問題解決_python

作者:呆萌的代Ma ? 更新時(shí)間: 2023-03-15 編程語言

解決方法

查看dataframe的數(shù)據(jù)類型,轉(zhuǎn)為數(shù)值類型即可:

dataframe = dataframe.astype("float")

或者逐列轉(zhuǎn)換:

for _i, _value in dataframe.iteritems():
    dataframe.loc[:, _i] = pd.to_numeric(_value)

問題復(fù)現(xiàn)

import pandas as pd
import numpy as np

dataframe = pd.DataFrame(np.random.random(size=(10, 5)))

這時(shí)計(jì)算.corr()會(huì)得到預(yù)期的結(jié)果:

在這里插入圖片描述

但如果轉(zhuǎn)為object類型就會(huì)變?yōu)榭罩担?/p>

在這里插入圖片描述

此時(shí)的dataframe.info()得到的結(jié)果為:

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

原文鏈接:https://blog.csdn.net/weixin_35757704/article/details/128481401

欄目分類
最近更新