Dictionary keys as pandas column labels

时间:2018-02-26 17:58:23

标签: python pandas numpy dictionary dataframe

Say I have a dictionary d. And I want to create a pandas DataFrame, with all keys in d as column labels. And the number of rows lets say is n. How would I go about this?

example

n = 4
d = {'what': 5, 'where': 10, 'who': 66}

I want something like this

what where who
0     0     0
0     0     0 
0     0     0
0     0     0

1 个答案:

答案 0 :(得分:3)

This is one way:

n = 4
d = {'what': 5, 'where': 10, 'who': 66}

df = pd.DataFrame(data=0, columns=d, index=range(n))

#    what  where  who
# 0     0      0    0
# 1     0      0    0
# 2     0      0    0
# 3     0      0    0