I have a dictionary where the key is two parts, one the index coordinate and the other the column coordinate. I would like to use this dictionary to populate a pandas DataFrame based on these coordinates.
For example my dictionary looks like this:
final = {('BUV395', 'BUV496'): 0, ('BUV395', 'BUV563'): 0, ('BUV395', 'BUV615'): 0, ('BUV395', 'BUV661'): 0, etc...
The input to my function is the pandas DataFrame with the original data - just to give context to the code below:
def matrix_all_pairs(df):
dataframe = pd.DataFrame(index=range(0,len(df.index.values)),columns=range(0,len(df.index.values)))
dataframe.columns = df.index.values
idx = list(df.index.values)
list_fluor = list(combinations(df.index.values, 2))
final = {}
for fluor in list_fluor:
if (r2_score(df.xs(fluor[0]), df.xs(fluor[1]))) < 0:
final[fluor] = 0
else:
final[fluor] = (r2_score(df.xs(fluor[0]), df.xs(fluor[1])))
for fluor, value in list_fluor:
x = value
dataframe.loc(idx.index(fluor[0]), fluor[1]) = x
dataframe.index = df.index.values
return(dataframe)
When I try to run this, it gives me "SyntaxError: can't assign to function call" for the line:
dataframe.loc(idx.index(fluor[0]), fluor[1]) = x
Is there a better way of doing this? I've seen multiple people say that populating an empty DataFrame using a loop is messy but I'm not sure how else I could do this?
I'm not sure how to post my data for people to work with - I'm new to this site.