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
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]
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)
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!