Python Pandas replicate rows in dataframe

前端 未结 5 1189
逝去的感伤
逝去的感伤 2020-11-29 20:07

If the data look like:

Store,Dept,Date,Weekly_Sales,IsHoliday
1,1,2010-02-05,24924.5,FALSE
1,1,2010-02-12,46039.49,TRUE
1,1,2010-02-19,41595.55,FALSE
1,1,201         


        
5条回答
  •  生来不讨喜
    2020-11-29 21:05

    Appending and concatenating is usually slow in Pandas so I recommend just making a new list of the rows and turning that into a dataframe (unless appending a single row or concatenating a few dataframes).

    import pandas as pd
    
    df = pd.DataFrame([
    [1,1,'2010-02-05',24924.5,False],
    [1,1,'2010-02-12',46039.49,True],
    [1,1,'2010-02-19',41595.55,False],
    [1,1,'2010-02-26',19403.54,False],
    [1,1,'2010-03-05',21827.9,False],
    [1,1,'2010-03-12',21043.39,False],
    [1,1,'2010-03-19',22136.64,False],
    [1,1,'2010-03-26',26229.21,False],
    [1,1,'2010-04-02',57258.43,False]
    ], columns=['Store','Dept','Date','Weekly_Sales','IsHoliday'])
    
    temp_df = []
    for row in df.itertuples(index=False):
        if row.IsHoliday:
            temp_df.extend([list(row)]*5)
        else:
            temp_df.append(list(row))
    
    df = pd.DataFrame(temp_df, columns=df.columns)
    

提交回复
热议问题