Paw in Data
Paw in Data

Reputation: 1604

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

Upvotes: 0

Views: 423

Answers (1)

Maximus
Maximus

Reputation: 1312

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

Upvotes: 1

Related Questions