5

I am calculating the correlation matrix for a dataset in Python Spyder using the command df.corr(). However, the output shows the matrix values only for the first two and the last two columns. What command should I use so that I can get the entire matrix?

I am using the pandas package for calculation the correlation matrix of a given dataset.

The code I used for calculating the correlation matrix is:

correlation_matrix = df.corr()
print(correlation_matrix)

The result of this is that the correlation matrix values for the first two columns and the last two columns are getting displayed. I want the entire matrix to be displayed.

4
  • Can't reproduce Commented Jan 23, 2019 at 9:37
  • 1
    I think it will only show the matrix for numerical data. Are your other column of object type> Commented Jan 23, 2019 at 9:46
  • Yes, some columns are of other types Commented Jan 23, 2019 at 9:58
  • can you add some sample data? Commented Jan 23, 2019 at 10:20

1 Answer 1

8

It is might be because of the default print options of the pandas package.

Try the following.

import pandas as pd
pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

Sometimes, when your data frame has non-numeric columns, then those would be excluded when computing correlation. This would result in lesser number of columns in the corr() output.

Sign up to request clarification or add additional context in comments.

2 Comments

I think this isn't OP's issue, since the default is to print more than two. @Mohit Motwani's answer in the comments seems more likely
Not sure. My thinking is that output of corr() would be a dataframe with numerical values alone but @Mohit's point is valid that the corr cannt computed for objects!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.