Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: create named columns in DataFrame from dict

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'

like image 914
anonuser0428 Avatar asked Dec 03 '13 00:12

anonuser0428


People also ask

Can a dictionary of dictionaries be used to create a pandas DataFrame?

We can create a dataframe using Pandas. DataFrame() method. Example: Create pandas Dataframe from the dictionary of dictionaries.

How do you create a DataFrame from a dictionary?

Method 1: Create DataFrame from Dictionary using default Constructor of pandas. Dataframe class. Method 2: Create DataFrame from Dictionary with user-defined indexes. Method 3: Create DataFrame from simple dictionary i.e dictionary with key and simple value like integer or string value.

What method creates a pandas DataFrame from a dictionary?

pandas. DataFrame. from_dict() can be used to create a pandas DataFrame from Dict (Dictionary) object. This method takes parameters data , orient , dtype , columns and returns a DataFrame.


3 Answers

You can iterate through the items:

In [11]: pd.DataFrame(list(my_dict.items()),
                      columns=['business_id','business_code'])
Out[11]: 
  business_id business_code
0         id2          val2
1         id3          val3
2         id1          val1
like image 63
Andy Hayden Avatar answered Oct 24 '22 10:10

Andy Hayden


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.

like image 12
cfelix Avatar answered Oct 24 '22 10:10

cfelix


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.

like image 4
Ninjakannon Avatar answered Oct 24 '22 08:10

Ninjakannon