LoginSignup
0
0

More than 5 years have passed since last update.

How to explode a column in pandas

Posted at
 def explode(self, df, explode_column, delimiter=","):
        assert explode_column[-1] is "s"
        df["id"] = df.index
        wdf = pd.DataFrame(df[explode_column].str.split(delimiter).tolist()).stack().reset_index()
        exploded_column = explode_column[:-1]
        wdf.columns = ["id", "depth", exploded_column] ## plural form to singular form
        wdf[exploded_column] = wdf[exploded_column].apply(lambda x: x.strip())  # trim
        wdf.drop("depth", axis=1, inplace=True)

        return pd.merge(df, wdf, on='id').drop(columns=["id", explode_column])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0