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:

data = {'wordA': [3, 2, 1, 0], 'wordB': [33, 12, 1, 8], 'wordC': [54, 10, 7, 3]} 
pd.DataFrame.from_dict(data, orient='index', columns=['col1', 'col2', 'col3', 'col4'])

To produce a DataFrame such as below:

        col1    col2    col3    col4
wordA   3       2       1       0
wordB   33      12      1       8
wordC   54      10      7       3

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.

word    count
wordA   4
wordB   4
wordC   4

How can I achieve this?

Advertisement

Answer

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

out = pd.DataFrame.from_dict({i: {'word': k, 'count': len(v)} 
                              for i, (k, v) in enumerate(data.items())}, 
                             orient='index')

or

out = (pd.DataFrame.from_dict({'count': {k: len(v) for k, v in data.items()}})
       .rename_axis('word').reset_index())

Output:

    word  count
0  wordA      4
1  wordB      4
2  wordC      4
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement