Home > Backend Development > Python Tutorial > How to Convert a Pandas DataFrame to a Dictionary with List Values?

How to Convert a Pandas DataFrame to a Dictionary with List Values?

Susan Sarandon
Release: 2024-12-14 08:44:10
Original
332 people have browsed it

How to Convert a Pandas DataFrame to a Dictionary with List Values?

Convert a Pandas DataFrame to a Dictionary

In many data manipulation tasks, extracting data from a Pandas DataFrame into a convenient format is necessary. One common need is to convert a DataFrame to a Python dictionary, where the elements of the first column become keys and the elements of other columns in the same row become values.

Example

Consider the following DataFrame:

    ID   A   B   C
0   p    1   3   2
1   q    4   3   2
2   r    4   0   9
Copy after login

We want to convert this DataFrame to a dictionary of the following form:

{'p': [1,3,2], 'q': [4,3,2], 'r': [4,0,9]}
Copy after login

Solution

To achieve this, we can utilize the to_dict() method provided by Pandas. However, to align the DataFrame into the desired format, we need to set the 'ID' column as the index and transpose the DataFrame using .T. Additionally, we specify the orient argument in to_dict() to output a list of values for each column.

The following code demonstrates this approach:

df.set_index('ID').T.to_dict('list')
Copy after login

This will produce the desired dictionary:

{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
Copy after login

Alternative Options

Beyond the default dict format, Pandas offers a range of options for outputting dictionaries using the orient argument:

  • dict: Column names as keys, values as dictionaries of index:data pairs
  • list: Keys are column names, values are lists of column data
  • series: Similar to 'list', but values are Series objects
  • split: Column names as keys, data values as values, and index labels as a separate key
  • records: Each row becomes a dictionary with column names as keys and data as values
  • index: Similar to 'records', but a dictionary of dictionaries with index labels as keys

The above is the detailed content of How to Convert a Pandas DataFrame to a Dictionary with List Values?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template