I’d like to convert a Python data frame (pandas or other) into a dictionary but the key should be the position (i,j). For example, have a dataframe like:
| col1 | col2 | col3 |
|---|---|---|
| 7 | 1 | 2 |
| 8 | 9 | 6 |
| 4 | 7 | 5 |
I'd like to return a dictionary with the key as the position of the value, so the 2nd row, 3rd column would be stored in a dictionary like below.
{(1,2): 6}
Is there a built-in function for this? Or do I need to iterate over the entire data frame? I have not yet found a way to get the position into a dictionary. From other threads, I've seen that you can extract the index with something like this (dataframe called 'df'):
df.stack().index.tolist()