I saw an answer to a question on converting a nested "2D" dictionary to a Pandas DataFrame. That would be a solution to my problem, but then I was wondering, whether I can the skip the intermediate step of generating a nested dictionary. Let's say my input input.txt
looks like this:
A B 1
A C 2
B C 3
Can I convert that to the following symmetric matrix with either Pandas or Numpy without having to generate an intermediate nested dictionary?
A B C
A 0 1 2
B 1 0 3
C 2 3 0
The nested dictionary that I want to avoid creating would be:
d = {'A':{'B':1,'C':2},'B':{'C':3}}
I tried this after reading the IO Tools documentation on "Reading an index with a MultiIndex":
import pandas as pd
df = pd.read_csv('input.txt', sep=' ', index_col=[0,1], header=None)
But I don't get a 2D heat map, when I do:
import matplotlib.pyplot as plt
plt.pcolor(df)
plt.imshow()