How to explode a list inside a Dataframe cell into separate rows

后端 未结 11 2158
天命终不由人
天命终不由人 2020-11-22 10:20

I\'m looking to turn a pandas cell containing a list into rows for each of those values.

So, take this:

If I\'d like to unpack and stack the value

11条回答
  •  失恋的感觉
    2020-11-22 11:06

    Similar to Hive's EXPLODE functionality:

    import copy
    
    def pandas_explode(df, column_to_explode):
        """
        Similar to Hive's EXPLODE function, take a column with iterable elements, and flatten the iterable to one element 
        per observation in the output table
    
        :param df: A dataframe to explod
        :type df: pandas.DataFrame
        :param column_to_explode: 
        :type column_to_explode: str
        :return: An exploded data frame
        :rtype: pandas.DataFrame
        """
    
        # Create a list of new observations
        new_observations = list()
    
        # Iterate through existing observations
        for row in df.to_dict(orient='records'):
    
            # Take out the exploding iterable
            explode_values = row[column_to_explode]
            del row[column_to_explode]
    
            # Create a new observation for every entry in the exploding iterable & add all of the other columns
            for explode_value in explode_values:
    
                # Deep copy existing observation
                new_observation = copy.deepcopy(row)
    
                # Add one (newly flattened) value from exploding iterable
                new_observation[column_to_explode] = explode_value
    
                # Add to the list of new observations
                new_observations.append(new_observation)
    
        # Create a DataFrame
        return_df = pandas.DataFrame(new_observations)
    
        # Return
        return return_df
    

提交回复
热议问题