Convert dictionaries to dataframe

if using all scalar values, you must pass an index
list of dictionaries to dataframe
nested dictionary to dataframe
pandas dataframe
dataframe to dictionary with one column as key
convert the dictionary into dataframe
pandas unpack dictionary
dataframe to dictionary by row

I am trying to convert this dictionary:

data = ({"Jan 2018":1000},{"Feb 2018":1100},{"Mar 2018":1400},{"Apr 2018":700},{"May 2018":800})
data

to dataframe like:

date       balance
Jan 2018   1000
Feb 2018   1100
Mar 2018   1400
Apr 2018   700
May 2018   800

I used the dataframe to convert, but it didn't give the format as above, how can i do it? Thank you!

pd.DataFrame.from_dict(data_c, orient='columns')

Here is my solution:

import pandas as pd

data = ({"Jan 2018":1000},{"Feb 2018":1100},{"Mar 2018":1400},{"Apr 2018":700},{"May 2018":800})

arr = [list(*d.items()) for d in data]

df = pd.DataFrame(arr, columns=['data', 'balance'])

you need get proper array from the tuple of dictionary before pass it to DataFrame.

pandas.DataFrame.from_dict — pandas 1.1.0 documentation, Construct DataFrame from dict of array-like or dicts. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters. Pandas DataFrame from_dict() method is used to convert Dict to DataFrame object. This method accepts the following parameters. data: dict or array like object to create DataFrame. orient: The orientation of the data. The allowed values are (‘columns’, ‘index’), default is the ‘columns’.

Try this

df = pd.DataFrame.from_dict({k: v for d in data for k, v in d.items()}, 
                             orient='index', 
                             columns=['balance']).rename_axis('date').reset_index()

Out[477]:
       date  balance
0  Jan 2018     1000
1  Feb 2018     1100
2  Mar 2018     1400
3  Apr 2018      700
4  May 2018      800

Convert Python dict into a dataframe, Pandas have built-in function for conversion of dict to data frame. Accepts a dict as argument and returns a dataframe with the keys of the dict as index and values as a column. Tool to Convert Dictionaries. In the last section of this tutorial, I’ll share with you the code to create a tool to convert dictionaries to DataFrames. The code is based on the tkinter module that can be used to create a Graphical User Interface (GUI) in Python. Here is the complete code:

From the documentation of from_dict

orient : {‘columns’, ‘index’}, default ‘columns’

The "orientation" of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’.

Since you want your keys to indicate rows, changing the orient to index will give the result your want. However first you need to put your data in a single dictionary. This code will give you the result you want.

data = ({"Jan 2018":1000},{"Feb 2018":1100},{"Mar 2018":1400},{"Apr 2018":700},{"May 2018":800})

d = {}
for i in data:
    for k in i.keys():
        d[k] = i[k]

df = pd.DataFrame.from_dict(d, orient='index')

Pandas DataFrame from_dict() - Dictionary to DataFrame, On Initialising a DataFrame object with this kind of dictionary, each item (Key / Value pair) in dictionary will be converted to one column i.e. key� This articles show you how to convert a Python dictionary list to a Spark DataFrame. The code snippets runs on Spark 2.x environments. Input The input data (dictionary list looks like the following): data = [{"Category": 'Category A', 'ItemID': 1, 'Amount': 12.40},

What you have there is a tuple of single-element dictionaries. This is unidiomatic, and poor design. If all the dictionaries correspond to the same columns, then a list of tuples would do just fine.


Solutions

I believe the currently accepted answer relies on there being only one key:value pair in each dictionary. That’s unfortunate, since it automatically excludes most situations where this design makes any sense.


If, hypothetically, the "tuple of 1-element dicts" couldn't be changed, here is how I would suggest doing things:

import pandas as pd
import itertools as itt

raw_data = ({"Jan 2018": 1000}, {"Feb 2018": 1100}, {"Mar 2018": 1400}, {"Apr 2018": 700}, {"May 2018": 800})

data = itt.chain.from_iterable(curr.items() for curr in raw_data)

df = pd.DataFrame(data, columns=['date', 'balance'])

Here is the sensible alternative to all this.

import pandas as pd

data = [("Jan 2018", 1000), ("Feb 2018", 1100), ("Mar 2018", 1400), ("Apr 2018", 700), ("May 2018", 800)]

df = pd.DataFrame(data, columns=['date', 'balance'])

df:

       date  balance
0  Jan 2018     1000
1  Feb 2018     1100
2  Mar 2018     1400
3  Apr 2018      700
4  May 2018      800

It would probably be even better if those dates were actual date types, not strings. I will change that later.

Python Pandas : How to create DataFrame from dictionary , Pandas offer several options to create DataFrames from lists or The “default” manner to create a DataFrame from python is to use a list of dictionaries. a DataFrame is that it is very easy to convert into other formats such as� Pandas : Convert Dataframe index into column using dataframe.reset_index() in python; Python: Find indexes of an element in pandas dataframe; Pandas : Convert Dataframe column into an index using set_index() in Python; Pandas: Convert a dataframe column into a list using Series.to_list() or numpy.ndarray.tolist() in python

Creating Pandas DataFrames from Lists and Dictionaries, We can convert a dictionary to a pandas dataframe by using the pd.DataFrame. from_dict() class-method. Example 1: Passing the key value as a list. pandas.DataFrame.from_dict¶ classmethod DataFrame.from_dict (data, orient = 'columns', dtype = None, columns = None) [source] ¶ Construct DataFrame from dict of array-like or dicts. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters data dict. Of the form {field : array-like} or {field

How to convert Dictionary to Pandas Dataframe?, Pandas DataFrame is a 2-dimensional labeled data structure with columns of Pandas DataFrame by lists of dicts. as dictionary keys column of a Pandas DataFrame � How to Convert Pandas DataFrame into a List? A pandas DataFrame can be converted into a Python dictionary using the DataFrame instance method to_dict(). The output can be specified of various orientations using the parameter orient . In dictionary orientation, for each column of the DataFrame the column value is listed against the row label in a dictionary.

Create a Pandas DataFrame from List of Dicts, items() to get a set-like object with the keys and values of dict . Use list(iterable) with iterable as the set-like object to convert it to a list. To create a DataFrame� At times, you may need to convert your list to a DataFrame in Python. In this post, I'll show you 3 examples to perform the conversion.

Comments
  • And if you replace in your data ( ) by [ ] ?
  • That's not a dictionary. Why are you using a tuple instead of a list, and why a bunch of dictionaries? That's the bigger issue here.
  • Doesn’t this solution rely on there being only 1 element in each dictionary?
  • @AlexanderCécile: it is just my preference to use dict comprehension. dict.update still needs a for loop to get each element of data to pass to it. Calling d_1.update(data) won't work because data is tuples of dicts.
  • Yeah, the loop is unfortunate. It just feels so odd seeing {k: v for k, v in....}.