Home >Backend Development >Python Tutorial >How to Convert a Pandas DataFrame to a Dictionary with List Values?
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.
Consider the following DataFrame:
ID A B C 0 p 1 3 2 1 q 4 3 2 2 r 4 0 9
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]}
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')
This will produce the desired dictionary:
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
Beyond the default dict format, Pandas offers a range of options for outputting dictionaries using the orient argument:
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!