Reputation: 1268
I have a list of dictionaries that look like this.
d = [{'a': 1, 'b': 2}, {'a': 2, 'b': 3}....]
I am trying to construct a DataFrame from this as follows:
df = pd.DataFrame(d, index=['a'])
But I get this error:
ValueError: Shape of passed values is (X, 2), indices imply (1, 2)
(X is a number of items I have in d)
Upvotes: 0
Views: 56
Reputation: 391
Because I cannot comment. See here Convert list of dictionaries to a pandas DataFrame
And Quang Hoang is correct. This is analogous to timeseries data. You want to set the index after the dataframe has been built.
df = pd.DataFrame(d).set_index('a')
The index argument in the constructor expects a list or a series so setting it as 'a' will mean there is only 1 row. However setting the index to the column 'a' after the dataframe has been built, sets the index to the series represented by the column 'a'. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
Upvotes: 1
Reputation: 323226
I will using from_records
pd.DataFrame.from_records(d,index='a')
Out[26]:
b
a
1 2
2 3
Upvotes: 2