Python Pandas replicate rows in dataframe

前端 未结 5 1164
逝去的感伤
逝去的感伤 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条回答
  •  -上瘾入骨i
    2020-11-29 20:53

    This is an old question, but since it still comes up at the top of my results in Google, here's another way.

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'col1':list("abc"),'col2':range(3)},index = range(3))
    

    Say you want to replicate the rows where col1="b".

    reps = [3 if val=="b" else 1 for val in df.col1]
    df.loc[np.repeat(df.index.values, reps)]
    

    You could replace the 3 if val=="b" else 1 in the list interpretation with another function that could return 3 if val=="b" or 4 if val=="c" and so on, so it's pretty flexible.

提交回复
热议问题