0
import pandas as pd

DF = pd.DataFrame(DICTIONARY, 
                  index = [r"$\lambda$="+str(i) for i in range(3)],
                  columns = [r"$\xi$="+str(j) for j in range(3)])

There are a few times when I have a dictionary (not very large) and try to convert it into a dataframe, the code above would yield one with each cell being NaN. Yet the code below works fine. I wonder what could be the difference?

DF = pd.DataFrame(DICTIONARY, index = [r"$\lambda$="+str(i) for i in range(3)])
DF.columns = [r"$\xi$="+str(j) for j in range(3)]

1 Answer 1

1

What are your dictionary keys? I am guessing the keys don't align to your columns.

In the second option you are letting pandas assign default column names and then overwriting them.

Something like the below code works when the column names align - but explicitly defining the columns parameter, in this case, adds no value because the dict key already provides the names.

DF = pd.DataFrame({1:1,2:2,3:3},
    index = [r"$\lambda$="+str(i) for i in range(3)],
    columns = [j+1 for j in range(3)])
Sign up to request clarification or add additional context in comments.

1 Comment

Aren't you a genius...^_^ You're right about the dictionary keys! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.