Reputation: 47
I want to make a customize correlation table, after a long script this is my final data look like:
ID a ... z
1 76.0 ... 1.033004
2 257.0 ... 0.629641
3 99.0 ... 0.672139
4 52.0 ... 1.007708
5 129.0 ... 1.080922
And this is the script I made to my customize correlation table for a vs all variable
pg.pairwise_corr(df_pvt, columns=[['a'], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append
pg.pairwise_corr(df_pvt, columns=[['b'], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append ......... #until variable z
How can I loop or select specific column number without typing variable name 'a' until 'z' ?
I am new in python. Thank you in advance
Upvotes: 0
Views: 210
Reputation: 2139
import pandas as pd
data = pd.DataFrame({"ID": [1,2,3,4,5], "a": [76.0, 257.0, 99.0,52.0,129.0], "z": [1.033004, 0.629641, 0.672139,1.007708, 1.080922]}, columns=["ID", "a", "z"])
true_columns = []
for index, row in data.iterrows():
true_col_list = [col for col in data.columns[1] if row[col]]
true_columns.append(",".join(true_col_list))
data["Columns"] = true_columns
print(data)
Upvotes: 1
Reputation: 670
As I'm not allowed to comment yet, I think this is what you're looking for. Correct me if I'm wrong.
for letter in 'abcdefghijklmnopqrstuvwxyz':
pg.pairwise_corr(df_pvt, columns=[letter], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append ....
edit: And I think, if you want to be cool and pythonic (I've seen this in many answers) you could not write out the alphabet yourself but import it with
import string
alphabet = string.ascii_lowercase # this equals 'abcdefghijklmnopqrstuvwxyz'
# then iterate over the alphabet
for letter in alphabet:
# do your stuff here something something[letter] something :P
Upvotes: 0