32

I have a dictionary object of the form:

my_dict = {id1: val1, id2: val2, id3: val3, ...}

I want to create this into a DataFrame where I want to name the 2 columns 'business_id' and 'business_code'.

I tried:

business_df = DataFrame.from_dict(my_dict,orient='index',columns=['business_id','business_code'])

But it says from_dict doesn't take in a columns argument.

TypeError: from_dict() got an unexpected keyword argument 'columns'

Ninjakannon
  • 3,283
  • 5
  • 42
  • 65
anonuser0428
  • 8,987
  • 18
  • 55
  • 81

5 Answers5

31

You can iterate through the items:

In [11]: pd.DataFrame(list(my_dict.iteritems()),
                      columns=['business_id','business_code'])
Out[11]: 
  business_id business_code
0         id2          val2
1         id3          val3
2         id1          val1
Andy Hayden
  • 291,328
  • 80
  • 565
  • 500
12

To get the same functionality as the documentation and avoid using code workarounds, make sure you're using the most recent version of Pandas. I recently encountered the same error when running a line of code from the Pandas tutorial:

pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]),orient='index', columns=['one', 'two', 'three'])

I checked the version of Pandas and found I was running version 22, when version 23 is available.

import pandas as pd
pd.__version__
Out[600]: '0.22.0'

I upgraded using pip:

c:\pip install --upgrade pandas

I confirmed my version updated to 23, and the same from_dict() code worked without error. No code modifications required.

cfelix
  • 166
  • 1
  • 6
4

From version 0.23.0, you can specify a columns parameter in from_dict:

my_dict = {id1: val1, id2: val2, id3: val3, ...}
prepared_dict = {i: x for i, x in enumerate(my_dict.items())}
df = pd.DataFrame.from_dict(prepared_dict, orient='index', columns=['business_id', 'business_code'])

Note: I also answered in kind on this similar question.

Ninjakannon
  • 3,283
  • 5
  • 42
  • 65
  • 1
    I tried this out using pandas 0.24.2 and Python 3.6.8 got the following error: `ValueError: Shape of passed values is (3, 1), indices imply (3, 2)` – anthls May 27 '19 at 02:51
  • @anthls That is not enough information to solve your issue, which I do not think is directly a result of `from_dict`. If you seek help on Stack Overflow, I would recommend asking a separate question. – Ninjakannon May 27 '19 at 10:57
  • Apologies if I wasn't clear - I wanted you and others to know I'd tried your answer but it didn't work for me. I used the following code (using pandas 0.24.2 and Python 3.6.8): `import pandas as pd my_dict = {"id1": "val1", "id2": "val2", "id3": "val3"} df = pd.DataFrame.from_dict(my_dict, orient='index', columns=['business_id', 'business_code'])` and got the error I mentioned (I've left out the full stack trace). I used the accepted answer and it worked so I don't require resolution of my issue but thought you might like to know. – anthls May 28 '19 at 00:47
  • @anthls I see what you mean now, thanks. I have updated the answer. – Ninjakannon May 28 '19 at 19:57
0

Do this:

create the dataframe

df = pd.DataFrame(data_as_2d_ndarray)

create a sorted list of column names from the dictionary - adjust the key karg as need to grab the sorting value from your dict, obvilous the dictionary the data must have consistent shapes

col_names = sorted(list(col_dict.iteritems()),key=lambda x:x[0])

reshape and set the column names

df.columns  = zip(*col_names)[1]
meyerson
  • 4,224
  • 1
  • 17
  • 18
0

This is with respect to TypeError you faced. As per Pandas documentation, from_dict will take the keyword 'columns' only if the orient = 'index'.

Karthik Sunil
  • 385
  • 3
  • 13