Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Look at the below pandas dataframe:

I make a column that appraises the number of citable documents per individual: 

Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita']

Top15['Citable docs per Capita'] = Top15['Citable documents'] / Top15['PopEst']

I need to know the correlation between's the quantity of citable documents per capita and also the energy supply per capita. So I utilize the .corr() technique (Pearson's relationship): 

data = Top15[['Citable docs per Capita','Energy Supply per Capita']]

correlation = data.corr(method='pearson')

I need to restore a single number, yet the outcome is: enter picture depiction here

1 Answer

0 votes
by (26.4k points)

Without actual information/data it is difficult to respond to the inquiry however I suppose you are searching for something like this: 

Top15['Citable docs per Capita'].corr(Top15['Energy Supply per Capita'])

That figures the correlation between's your two sections 'Citable docs per Capita' and 'Energy Supply per Capita'. 

For example:

import pandas as pd

df = pd.DataFrame({'A': range(4), 'B': [2*i for i in range(4)]})

   A  B

0  0  0

1  1  2

2  2  4

3  3  6

Then

df['A'].corr(df['B'])

gives 1 as expected.

Just if you try to change the value, example

df.loc[2, 'B'] = 4.5

   A    B

0  0  0.0

1  1  2.0

2  2  4.5

3  3  6.0

the command

df['A'].corr(df['B'])

returns

0.99586

which is still near 1, true to form.

In the event that you apply .corr straightforwardly to your dataframe, it will restore all pairwise correlations between's your columns; that is the reason you at that point notice 1s at the diagonal of the matrix (every column is completely connected with itself). 

df.corr()

will therefore return

          A         B

A  1.000000  0.995862

B  0.995862  1.000000

In the realistic you show, just the upper left corner of correlation matrix is addressed (I expect).

Are you willing to learn python to get expertise in the topics of python? Kindly, Join the python certification course and get certified

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...