In Python how to do Correlation between Multiple Columns more than 2 variables?

痴心易碎 提交于 2020-01-02 10:20:52

问题


I have a Pandas Dataframe like so:

id    cat1    cat2    cat3    num1    num2
1     0       WN      29      2003    98
2     1       TX      12      755     76
3     0       WY      11      845     32
4     1       IL      19      935     46

I want to find out the correlation between Cat1 and column cat3, num1 and num2 or between cat1 and num1 and num2 or between cat2 and cat1, cat3,num1,num2

When I use df.corr() it gives Correlation between all the columns in the dataframe, but I want to see Correlation between just these selective columns detailed above.

How do I do that in Python pandas?

A Thousand thanks in advance for your answers.


回答1:


I tried the following and it worked :

features1=list(['cat1','cat2','cat3'])
features2=list(['Cat1', 'Cat2','num1','num2'])

df[features1].corr()
df[features2].corr()

Good way to select the columns based on the need when you have a very high number of variables in your dataset.



来源:https://stackoverflow.com/questions/42128462/in-python-how-to-do-correlation-between-multiple-columns-more-than-2-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!