Assigning column names while creating dataframe results in nan values

Viewed 700

I have a list of dict which is being converted to a dataframe. When I attempt to pass the columns argument the output values are all nan.

# This code does not result in desired output

l = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
pd.DataFrame(l, columns=['c', 'd'])

    c   d
0   NaN NaN
1   NaN NaN
# This code does result in desired output

l = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
df = pd.DataFrame(l)
df.columns = ['c', 'd']
df

    c   d
0   1   2
1   3   4

Why is this happening?

1 Answers

Because if pass list of dictionaries from keys are created new columns names in DataFrame constructor:

l = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
print (pd.DataFrame(l))
   a  b
0  1  2
1  3  4

If pass columns parameter with some values not exist in keys of dictionaries then are filtered columns from dictonaries and for not exist values are created columns with missing values with order like values in list of columns names:

#changed order working, because a,b keys at least in one dictionary
print (pd.DataFrame(l, columns=['b', 'a']))
   b  a
0  2  1
1  4  3

#filtered a, d filled missing values - key is not at least in one dictionary
print (pd.DataFrame(l, columns=['a', 'd']))
   a   d
0  1 NaN
1  3 NaN

#filtered b, c filled missing values - key is not at least in one dictionary
print (pd.DataFrame(l, columns=['c', 'b']))
    c  b
0 NaN  2
1 NaN  4

#filtered a,b, c, d filled missing values - keys are not at least in one dictionary
print (pd.DataFrame(l, columns=['c', 'd','a','b']))
    c   d  a  b
0 NaN NaN  1  2
1 NaN NaN  3  4

So if want another columns names you need rename them or set new one like in your second code.

Related