Pandas Column of Lists: Creating a Row for Each List Element
In Pandas dataframes, it may be necessary to convert a column containing lists into multiple rows, where each element of the list occupies its own row. To achieve this, there are two viable options.
Pandas>=0.25's Built-In explode Method
For Pandas versions 0.25 and later, the .explode() method has been introduced specifically for this purpose. It efficiently converts lists into individual rows.
df = pd.DataFrame({'trial_num': [1, 2, 3, 1, 2, 3], 'subject': [1, 1, 1, 2, 2, 2], 'samples': [list(np.random.randn(3).round(2)) for i in range(6)] }) df.explode('samples').reset_index(drop=True) # Resetting the index for clarity
This method handles mixed columns containing lists and scalars, as well as empty lists and NaNs. However, it is important to note that explode operates on a single column at a time.
Custom Function for Pandas <0.25
For earlier versions of Pandas, a custom function can be utilized:
def explode_list_column(df, column): # Create an empty list to store the expanded rows exploded_rows = [] # Iterate through each cell in the specified column for row in df[column]: # Iterate through each element in the list for element in row: # Add a new row to the list, copying data from the current row and adding the new element exploded_rows.append(list(row) + [element]) # Convert the expanded rows into a DataFrame return pd.DataFrame(exploded_rows, columns=df.columns + ['list_element'])
This function takes a DataFrame and the name of the column containing lists as parameters, and it returns a new DataFrame with a column for each list element.
# Applying the exploding function exploded_df = explode_list_column(df, 'samples')
The above is the detailed content of How Can I Transform a Pandas Column of Lists into Multiple Rows?. For more information, please follow other related articles on the PHP Chinese website!