How to fill in rows with repeating data in pandas?

前端 未结 7 513
感情败类
感情败类 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:22

    The cycle method from itertools is good for repeating a common pattern.

    from itertools import cycle
    
    seq = cycle([1, 2, 3])
    df['Seq'] = [next(seq) for count in range(df.shape[0])]
    

提交回复
热议问题