repeating the rows of a data frame

后端 未结 2 989
心在旅途
心在旅途 2020-12-19 11:07

I\'m trying repeat the rows of a dataframe. Here\'s my original data:

pd.DataFrame([
        {\'col1\': 1, \'col2\': 11, \'col3\': [1, 2] },
        {\'col1\         


        
2条回答
  •  甜味超标
    2020-12-19 12:07

    You can also use reindex and index.repeat

    df = df.reindex(df.index.repeat(df.col3.apply(len)))
    
    df = df.reset_index(drop=True).drop("col3", axis=1)
    # To reset index and drop col3 
    
    # Output:
    
       col1  col2
    0   1     11
    1   1     11
    2   2     22
    3   2     22
    4   2     22
    5   3     33
    6   4     44
    7   4     44
    8   4     44
    9   4     44
    

提交回复
热议问题