Split (explode) pandas dataframe string entry to separate rows

后端 未结 22 4182
一向
一向 2020-11-21 05:03

I have a pandas dataframe in which one column of text strings contains comma-separated values. I want to split each CSV field and create a new row per entry (as

22条回答
  •  甜味超标
    2020-11-21 05:17

    I have come up with the following solution to this problem:

    def iter_var1(d):
        for _, row in d.iterrows():
            for v in row["var1"].split(","):
                yield (v, row["var2"])
    
    new_a = DataFrame.from_records([i for i in iter_var1(a)],
            columns=["var1", "var2"])
    

提交回复
热议问题