Как сделать датафрейм из словаря в python
Перейти к содержимому

Как сделать датафрейм из словаря в python

  • автор:

Create Pandas DataFrame from Python dictionary

This article shows how to convert a Python dictionary to pandas DataFrame. It covers the creating DataFrame from all types of dictionaries using the DataFrame constructor and from_dict() method.

And at the end of this article, we summarize the usage of both ways with the comparison. So stay tuned…

Python dictionary is the data structure that stores the data in key-value pairs. By converting data from dictionary format to DataFrame will make it very competent for analysis by using functions of DataFrame.

There are multiple ways to convert Python dictionary object into Pandas DataFrame. Majorly used ways are,

  1. DataFrame constructor
  2. from_dict()

Table of contents

Create DataFrame from dict using constructor

DataFrame constructor can be used to create DataFrame from different data structures in python like dict , list, set, tuple, and ndarray .

In the below example, we create a DataFrame object using dictionary objects contain student data.

Pandas DataFrame from Python Dictionary

When you convert a dict to DataFrame by default, all the keys of the dict object becomes columns, and the range of numbers 0, 1, 2,…,n is assigned as a row index.


DataFrame from dict with required columns only

While converting the whole dict to DataFrame, we may need only some of the columns to be included in the resulting DataFrame.

We can select only required columns by passing list column labels to columns=[‘col1’, ‘col2’] parameter in the constructor.


In the case of student DataFrame for analyzing the annual score, we need only “student name” and “marks” whereas the “age” column is not required. We can select only required columns, as shown in the below example.


DataFrame from dict with user-defined indexes

In pandas DataFrame, each row has an index that is used to identify each row. In some cases, we need to provide a customized index for each row. We can do that while creating the DataFrame from dict using the index parameter of the DataFrame constructor.

The default index is a range of integers starting from 0 to a number of rows. We can pass a list of the row indexes as index=[‘index1′,’index2’ ] to the dataFrame constructor.


In the below example, we have given a customer index for each student, making it more readable and easy to access the row using it.


DataFrame from dict by changing the column data type

By default, while creating a DataFrame from dict using constructor, it keeps the original data type of the values in dict. But, if we need to change the data type of the data in the resulting DataFrame, we can use the dtype parameter in the constructor.

Only one data type is allowed to specify as dtype=’data_type’ which will be applicable for all the data in the resultant DataFrame. If we do not force such a data type, it internally infers from the Data.

Note: It changes the data type only if it is compatible with the new data type. Otherwise, it keeps the original data type.


As you can see below example, we are trying to change the data type to float64 for all the columns. But, it changes the data type of “age” and “marks” columns only to float64 even though the “marks” column type was “object“. But, the “name” column type is not changed because string values in that column cannot be converted to float64.


DataFrame from dict with a single value

If we have a dict with only single values for each key and need to convert such dict to the DataFrame, we can use the DataFrame constructor.

In such a case, it converts the dict to DataFrame as we have seen before, like keys of the dict will be column labels and values will be the column data. But, we must provide the index parameter to give the row index. Else it throws an error,

ValueError: If using all scalar values, you must pass an index


In the below example, we have provided the customized index=[‘stud1’] to the DataFrame.


DataFrame from dict with key and value as a column

Suppose we have a dictionary object where the key is the student’s name, and the value is the student’s marks. And we want the keys in one column and all the values in another column of the DataFrame.

For that, rather than passing a whole dict object, we need to pass each key-value pair in the dictionary to the DataFrame constructor to create a new DataFrame.

We can get the entry of key-value pair using dict.items() and pass that function to the constructor.


As shown in the below example, we need to pass an entry of key-value to the constructor and give column labels using columns parameter.


Create DataFrame from list of dict

For the sake of our understanding, consider the case where each school stores data of students into the dictionary data structure. Each school store different information about students. Like, some school stores student’s hobby whereas some school only stores academic information. If we want to analyze data of all the students from the city, we need to gather all this information into the DataFrame.

To convert such a list of dict from different schools can be converted to a single DataFrame using either DataFrame.from_dict() function or DataFrame constructor.

By default, keys of all the different dictionary objects are converted into columns of resultant DataFrame. It handles the missing keys by adding NaN where the values for the column are missing.


Let’s see how we can use a constructor to create DataFrame from different dictionary objects.


The from_dict() function

This is another way of creating DataFrame from a Python dictionary using DataFrame.from_dict() method.

Note: This method is useful for the cases when you need to transpose the DataFrame i.e. when we need the keys in the dictionary object as rows in the resultant DataFrame. In all the other cases DataFrame constructor should be preferred.

  1. data : It takes dict , list , set , ndarray , Iterable , or DataFrame as input. An empty DataFrame will be created if it is not provided. The resultant column order follows the insertion order.
  2. orient : (Optional) If the keys of the dict should be the rows of the DataFrame, then set orient = index else set it to column (Default) if the keys should be columns of the resultant DataFrame.
  3. dtype : (Optional) data type to force on resulting DataFrame. Only a single data type is allowed. If not given, then it’s inferred from the data.
  4. columns : (Optional) Only be used in case of orient=»index» to specify column labels in the resulting DataFrame. Default column labels are range of integer i.e. 0,1,2…n. Note: If we use the columns parameter with orient=’columns’ it throws an ValueError: cannot use columns parameter with orient=’columns’

DataFrame from dict with dict keys as a row

It is used to transpose the DataFrame, i.e., when keys in the dictionary should be the rows in the resultant DataFrame. We can change the orientation of the DataFrame using a parameter orient=»index» in DataFrame.from_dict() .


In the below example, keys “name“, “age“, and “marks” becomes row indexes in the DataFrame, and values are added in respective rows. New column labels are provided using columns parameter.


DataFrame from dict where values are variable-length lists

It is a widespread use case in the IT industry where data is stored in the dictionary with different values against each key.

If such a dictionary object needs to be converted into the DataFrame such that keys and values will be added as columns in DataFrame. Then it can be done using chaining of DataFrame.from_dict() , stack() , and reset_index() functions.


Here, we have dict with values are of different sizes and still we need to add all the key-values into a DataFrame.


DataFrame from dict nested dict

In this section, we cover the complex structure of the dictionary object where we have a hierarchical structure of the dictionary i.e. one dictionary object into another dictionary object.

In the below example, we have a student dictionary object where student data categorized by their grades and further divided as per their class. Such a dictionary object is converted into the multi-index DataFrame using DataFrame.from_dict() by iterating over each key and its values and parameter orient=’index’ .


DataFrame constructor vs from_dict()

The below table summarizes all the cases of converting dict to the DataFrame that we have already discussed in this article. It shows the comparison of using the DataFrame constructor and DataFrame.from_dict() method.

It will help you to choose the correct function for converting the dict to the DataFrame.

Use Case DataFrame
Custom column names Yes No
custom index Yes No
dict with a single value Yes No
list of dict Yes Yes
handle missing keys Yes Yes
keys and values as columns Yes Yes
change datatype Yes Yes
Orient=column(Keys as columns) Yes Yes
Orient=index(Keys as rows) No Yes
Multi-index DataFrame No Yes

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

Как преобразовать словарь в Pandas DataFrame(примеры 2)

Вы можете использовать один из следующих методов для преобразования словаря в Python в кадр данных pandas:

Способ 1: используйте dict.items()

Способ 2: использовать from_dict()

Оба метода дают одинаковый результат.

В следующих примерах показано, как использовать каждый метод на практике.

Пример 1. Преобразование словаря в фрейм данных с помощью dict.items()

Предположим, у нас есть следующий словарь в Python:

Мы можем использовать следующий код для преобразования этого словаря в DataFrame pandas:

Мы также можем использовать функцию type() , чтобы подтвердить, что результатом является DataFrame pandas:

Пример 2: преобразование словаря в фрейм данных с помощью from_dict()

Предположим, у нас есть следующий словарь в Python:

Мы можем использовать следующий код для преобразования этого словаря в DataFrame pandas:

Мы также можем использовать функцию type() , чтобы подтвердить, что результатом является DataFrame pandas:

Обратите внимание, что этот метод дает точно такой же результат, как и предыдущий метод.

Дополнительные ресурсы

В следующих руководствах объясняется, как выполнять другие распространенные задачи в pandas:

Как преобразовать словарь Python в Pandas DataFrame

Как преобразовать словарь Python в Pandas DataFrame

Мы познакомим вас с методом преобразования Pythonского отрицательного словаря в Pandas datafarme , а также с такими опциями, как наличие ключей в качестве столбцов и значений в качестве страниц и преобразование вложенного отрицательного словаря в DataFrame .

Мы также внедрим другой подход, используя pandas.DataFrame.from_dict , мы свяжем это с любым методом переименования , а также зададим имена индексов и столбцов одним махом.

Please enable JavaScript

Метод преобразования отрицательного в Pandas DataFame

Pandas DataFrame конструктор pd.DataFrame() преобразует словарь в DataFrame , если в качестве аргумента конструктора указан элементы словаря, а не сам словарь.

Клавиши и значения словаря преобразуются в две колонки DataFrame с именами столбцов, как указано в опциях столбцы .

Метод преобразования ключей в столбцы и значений в строки значения в Pandas DataFrame

Мы можем просто заключить словарь в скобки и удалить название столбца из приведенного выше кода вот так:

Мы используем панды понимания противоречий с concat , чтобы совместить все отрицания , а затем передадим список, чтобы дать новое название столбцам.

Рассмотрим следующий код,

pandas.DataFrame().from_dict() метод для преобразования dict в DataFrame

Мы будем использовать from_dict для преобразования dict в DataFrame , здесь мы устанавливаем orient=’index’ для использования ключей словаря в качестве строк и применяем метод raname() для изменения имени столбца.


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

orient <‘columns’, ‘index’, ‘tight’>, 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’. If ‘tight’, assume a dict with keys [‘index’, ‘columns’, ‘data’, ‘index_names’, ‘column_names’].

New in version 1.4.0: ‘tight’ as an allowed value for the orient argument

Data type to force after DataFrame construction, otherwise infer.

columns list, default None

Column labels to use when orient=’index’ . Raises a ValueError if used with orient=’columns’ or orient=’tight’ .

DataFrame from structured ndarray, sequence of tuples or dicts, or DataFrame.

DataFrame object creation using constructor.

Convert the DataFrame to a dictionary.

By default the keys of the dict become the DataFrame columns:

Specify orient=’index’ to create the DataFrame using dictionary keys as rows:

When using the ‘index’ orientation, the column names can be specified manually:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *