Home > Backend Development > Python Tutorial > How to Group Pandas DataFrame Rows and Transform Column Values into Lists?

How to Group Pandas DataFrame Rows and Transform Column Values into Lists?

Barbara Streisand
Release: 2024-12-18 13:44:17
Original
866 people have browsed it

How to Group Pandas DataFrame Rows and Transform Column Values into Lists?

Method to Group DataFrame Rows to Create Lists in GroupBy

In the realm of data manipulation using pandas, it's often necessary to manipulate DataFrame rows into specific formats. One common requirement is to group rows based on a particular column and retrieve the values from another column as lists.

Consider a DataFrame with columns 'a' and 'b', as shown below:

a b
A 1
A 2
B 5
B 5
B 4
C 6
Copy after login

The goal is to transform this DataFrame into a new one where the rows are grouped by column 'a', and the values from column 'b' are converted into lists for each group. The desired output would look like:

A [1, 2]
B [5, 5, 4]
C [6]
Copy after login

To achieve this, we can leverage the pandas 'groupby' and 'apply' functions, as demonstrated below:

# Import the pandas library
import pandas as pd

# Create a DataFrame from the provided data
df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})

# Group the DataFrame by column 'a'
grouped = df.groupby('a')

# Apply the list function to each group to get the 'b' values as lists
group_b_lists = grouped['b'].apply(list)

# Reset the index of the resulting Series to obtain a DataFrame
df_result = group_b_lists.reset_index(name='b_lists')

# Print the transformed DataFrame
print(df_result)
Copy after login

This code effectively groups the original DataFrame by column 'a', applies the list function to each group, and assigns the resulting lists to a new column called 'b_lists'. The resulting DataFrame is then printed to display the desired output.

The above is the detailed content of How to Group Pandas DataFrame Rows and Transform Column Values into Lists?. 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