Skip to content
Advertisement

Why can’t I define column names when I create the dataframe with pandas?

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)]

Advertisement

Answer

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)])
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement