split rows in pandas dataframe

后端 未结 4 741
無奈伤痛
無奈伤痛 2021-01-05 16:17

i stuck with the problem how to devide pandas dataframe by row,

i have similar dataframe with column where values separated by \\r\\n and they are in one cell,

4条回答
  •  囚心锁ツ
    2021-01-05 16:57

    You can do:

    df["Shape"]=df["Shape"].str.split("\r\n")
    print(df.explode("Shape").reset_index(drop=True))
    

    Output:

       Color    Shape   Price
    0   Green   Rectangle   10
    1   Green   Triangle    10
    2   Green   Octangle    10
    3   Blue    Rectangle   15
    4   Blue    Triangle    15
    

提交回复
热议问题