Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rearranging data from a table in python to correlate columns

I got a table that look like this:

code year month Value A Value B
1 2020 1 120 100
1 2020 2 130 90
1 2020 3 90 89
1 2020 4 67 65
100 2020 10 90 90
100 2020 11 115 100
100 2020 12 150 135

I would like to know if there’s a way to rearrange the data to find the correlation between A and B for every distinct code.

What I’m thinking is, for example, getting an array for every code, like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[(A1,A2,A3...,A12),(B1,B2,B3...,B12)]

where A and B is the values for the respective month, and then I could see the correlation between these two columns. Is there a way to make this dynamic?

>Solution :

IIUC, you don’t need to re-arrange to get the correlation for each "code". Instead, try with groupby:

>>> df.groupby("code").apply(lambda x: x["Value A"].corr(x["Value B"]))
code
1      0.830163
100    0.977093
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading