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
答案 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