def correlations(group):
# Remove columns
group = (
group.reset_index()
.drop(columns=["RNA1_Approved_Symbol"], level=0)
.set_index("RNA1_ID")
)
# compute correlations
correlation = group.T.corr()
# rename pair column
correlation = correlation.reset_index().rename(
columns={"RNA1_ID": "RNA1_ID_new"}
)
# Remove NAs
corr_result = (
correlation.melt(value_name="Correlation", id_vars=["RNA1_ID_new"])
.set_index(["RNA1_ID", "RNA1_ID_new"])
.dropna()
)
return corr_result
def correlation_1(data):
## Create Pivot table
pivot_library = data.pivot(
index=["RNA1_Approved_Symbol", "RNA1_ID"], columns=["RNA2_ID"]
)
# Measure correlations between four id's
return (
pivot_library.groupby("RNA1_Approved_Symbol")
.apply(correlations)
.reset_index()
.dropna()
)
I am using the above function to calculate the correlation
data = df1
correlations = correlation_1(df1)
I want to automate the above function when I am using different data frame where there is RNA2_Approved_Symbol
instead of RNA1_Approved_Symbol
and RNA2_ID
instead of RNA1_ID
. Like using index 1 and index 2.