How to fill in rows with repeating data in pandas?

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

    How general of a solution are you looking for? I tried to make this a little less hard-coded:

    import numpy as np
    import pandas 
    
    df = pandas.DataFrame(np.arange(1,7), columns=['first'])
    
    base = [1, 2, 3]
    df['second'] = base * (df.shape[0]/len(base))
    print(df.to_string())
    
    
       first  second
    0      1       1
    1      2       2
    2      3       3
    3      4       1
    4      5       2
    5      6       3
    
    0 讨论(0)
提交回复
热议问题