Skip to content
Advertisement

Create a customised pandas dataframe from items in a dictionary

I want to create a customised pandas DataFrame from items in dictionary. For example I can convert a dictionary to a DataFrame using pandas’ from_dict() function:

JavaScript

To produce a DataFrame such as below:

JavaScript

However what I want is to have only 2 columns, such as below, where the word column returns the dictionary keys and count column returns a count of the dictionary values contained in the list.

JavaScript

How can I achieve this?

Advertisement

Answer

You could change the dictionary that you pass to the constructor:

JavaScript

or

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement