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'
We can create a dataframe using Pandas. DataFrame() method. Example: Create pandas Dataframe from the dictionary of dictionaries.
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.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With