How to fill in rows with repeating data in pandas?

前端 未结 7 447
感情败类
感情败类 2021-01-02 02:59

In R, when adding new data of unequal length to a data frame, the values repeat to fill the data frame:

df <- data.frame(first=c(1,2,3,4,5,6))
df$second &         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 03:26

    Seems there is no elegant way. This is the workaround I just figured out. Basically create a repeating list just bigger than original dataframe, and then left join them.

    import pandas
    df = pandas.DataFrame(range(100), columns=['first'])
    repeat_arr = [1, 2, 3]
    df = df.join(pandas.DataFrame(repeat_arr * (len(df)/len(repeat_arr)+1),
        columns=['second']))
    

提交回复
热议问题