0

I have been searching for hours. I have 190 columns of pivot table to loop on my script I have this script:

corr = pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[0]], list(df_pvt.columns)], method='pearson')[['X','Y','r']]

this provide output:

                                          X  ...      r
0    CORSEC_Mainstream Media_Negative Count  ...  1.000
1    CORSEC_Mainstream Media_Negative Count  ...  0.960
2    CORSEC_Mainstream Media_Negative Count  ... -0.203
3    CORSEC_Mainstream Media_Negative Count  ... -0.446
4    CORSEC_Mainstream Media_Negative Count  ...  0.488
..                                      ...  ...    ...
179  CORSEC_Mainstream Media_Negative Count  ... -0.483
180  CORSEC_Mainstream Media_Negative Count  ... -0.487
181  CORSEC_Mainstream Media_Negative Count  ...  0.145
182  CORSEC_Mainstream Media_Negative Count  ...  0.128
183  CORSEC_Mainstream Media_Negative Count  ...  0.520

[184 rows x 3 columns]

I want to append 189 other columns to my script, but this script keep providing 2 appended variables and keep replacing until the 189th variables

for var in list(range(1,189)):
    corr_all = corr.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[var]], list(df_pvt.columns)], method='pearson')[['X','Y','r']])
    print(corr_all)

Any advice?

Edit:

Its work like this:

corr = pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[0]], list(df_pvt.columns)], method='pearson')[['X','Y','r']]
corr_1 = corr.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[1]], list(df_pvt.columns)], method='pearson')[['X','Y','r']])
corr_2 = corr_1.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[2]], list(df_pvt.columns)], method='pearson')[['X','Y','r']])

But how I loop it until the corr_189?

2 Answers 2

1

You can try making 189 lists of values (Pearson coefficients) for each of your 189 columns, and then concatenate the columns with " df_final " which would be the dataframe containing all the 190 columns :

corr = pd.DataFrame(corr)
df_final = pd.DataFrame()

for k in range(189):
    list_Pearson_k = 'formula to compute a list of pearson values'                    
    df_list_k = pd.DataFrame(list_Pearson_k)
    df_final = pd.concat([corr,df_list_k ], axis = 1) 
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry is that mean I need to make 189 rows of script to get 189 lists?
Fortunately no! I meant that you can do a loop, like in my answer, and that you could concatenate the column of pearson values at each iteration like this : corr = pd.DataFrame(corr) for k in range(189): list_Pearson_k = 'formula to compute a list of pearson values' df_list_k = pd.DataFrame(list_Pearson_k) df_final = pd.concat([corr,df_list_k ], axis = 1) This way you can get a df_final with your 190 columns of pearson values. If you want I can try to implement it but only in a few hours!
I changed my answer you can check that : instead of 'formula to compute a list of pearson values' you can put " pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[2]], list(df_pvt.columns)], method='pearson')[['X','Y','r']]" but I didn't try to run it
0

Python list append method returns None.

Change your code to this:-

corr_all = []
for var in range(1,189):
    corr_all.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[var]], list(df_pvt.columns)], method='pearson')[['X','Y','r']])
    print(corr_all)

This should help.

4 Comments

list(range(1, 189)) is a bit silly, because range() gives you an in-order iterable and then that's turned into a list which.... you iterate over. Remove the call to list() and the solution is more pythonic.
@Dhaval Taunk Thanks but Its still provide 2 columns combination at the end, how to generate all combination in 1 result?
its work with this, but I should make like 189 rows like this. How to loop this? corr = pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[0]], list(df_pvt.columns)], method='pearson')[['X','Y','r']] corr_1 = corr.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[1]], list(df_pvt.columns)], method='pearson')[['X','Y','r']]) corr_2 = corr_1.append(pg.pairwise_corr(df_pvt, columns=[[df_pvt.columns[2]], list(df_pvt.columns)], method='pearson')[['X','Y','r']])
@Irwan, sorry I didn't understand your question. Can you please explain properly?

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.