Popular Data Science Questions - Most Used & Most Viewed

Screen Link:

My Code:

tag_count = pd.DataFrame.from_dict(tag_count, orient='index',
                                  columns=['tag', 'count'])

What I expected to happen:
As per my understanding above code should create tag_count DataFrame with 2 columns, named ‘tag’ and ‘count’; ‘tag’ column should be populated with the keys from tag_count dictionary and ‘count’ column should be populated with data from key-values from the dictionary.

What actually happened:

But am getting the following error:

TypeErrorTraceback (most recent call last)
<ipython-input-8-f03d0c393b77> in <module>()
      1 tag_count = pd.DataFrame.from_dict(tag_count, orient='index',
----> 2                                   columns=['tag', 'count'])
      3 tag_count

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

Hey @dash.debasmita

Welcome to DQ Community!

can you share the structure of the tag_count dictionary - from which you are trying to create the dataframe or the whole notebook :grin:

I am unable to replicate this error. So not sure what part of the code needs tweaking in your case.

I am able to create the dataframe that you are trying though, using this code:

tag_count = {"test_tag1": 20, "test_tag2": 10, "test_tag3": 15}
tag_count = pd.DataFrame.from_dict(tag_count, orient="index") #, columns=['tag', 'count'])
tag_count.reset_index().rename(columns={"index":"tags", 0:"count"})

gives a result like this:

1 Like

Thanks for replying.
Earlier, using the ‘columns’ attribute in DataFrame.from_dict() was throwing the error shown above. But renaming the columns worked just fine and I was able to proceed.

Though, am not getting the error anymore and both the methods are working just fine.

Sorry to bother.


Hey, Dash.

In the documentation for pandas.DataFrame.from_dict it says that the columns parameter came with version 0.23 of pandas.

If you print __import__("pandas").__version__ in the Dataquest app, you’ll see that it uses version 0.22.

It should run fine locally, just as it did for Rucha.

I actually tried to use something like your solution at first, and later realized our version doesn’t support it :frowning:

1 Like

Got it Bruno.
Thanks for the clarification.