expand pandas dataframe into separate rows

Solutions on MaxInterview for expand pandas dataframe into separate rows by the best coders in the world

showing results for - "expand pandas dataframe into separate rows"
Mattia
08 Sep 2019
1def explode(df, lst_cols, fill_value='', preserve_index=False):
2    # make sure `lst_cols` is list-alike
3    if (lst_cols is not None
4        and len(lst_cols) > 0
5        and not isinstance(lst_cols, (list, tuple, np.ndarray, pd.Series))):
6        lst_cols = [lst_cols]
7    # all columns except `lst_cols`
8    idx_cols = df.columns.difference(lst_cols)
9    # calculate lengths of lists
10    lens = df[lst_cols[0]].str.len()
11    # preserve original index values    
12    idx = np.repeat(df.index.values, lens)
13    # create "exploded" DF
14    res = (pd.DataFrame({
15                col:np.repeat(df[col].values, lens)
16                for col in idx_cols},
17                index=idx)
18             .assign(**{col:np.concatenate(df.loc[lens>0, col].values)
19                            for col in lst_cols}))
20    # append those rows that have empty lists
21    if (lens == 0).any():
22        # at least one list in cells is empty
23        res = (res.append(df.loc[lens==0, idx_cols], sort=False)
24                  .fillna(fill_value))
25    # revert the original index order
26    res = res.sort_index()
27    # reset index if requested
28    if not preserve_index:        
29        res = res.reset_index(drop=True)
30    return res