Expand pandas DataFrame column into multiple rows

前端 未结 7 1629
星月不相逢
星月不相逢 2020-12-05 04:50

If I have a DataFrame such that:

pd.DataFrame( {\"name\" : \"John\", 
               \"days\" : [[1, 3, 5, 7]]
              })
<
7条回答
  •  一整个雨季
    2020-12-05 05:21

    A 'native' pandas solution - we unstack the column into a series, then join back on based on index:

    import pandas as pd #import
    x2 = x.days.apply(lambda x: pd.Series(x)).unstack() #make an unstackeded series, x2
    x.drop('days', axis = 1).join(pd.DataFrame(x2.reset_index(level=0, drop=True))) #drop the days column, join to the x2 series
    

提交回复
热议问题