how to add multiple rows in a specified position with modification in python?

后端 未结 1 1238
耶瑟儿~
耶瑟儿~ 2021-01-23 12:35

I have a data frame where I want to replicate and rows in the following manner

 d=pd.DataFrame({"col1":["a","b","c","         


        
相关标签:
1条回答
  • 2021-01-23 12:54

    IIUC, You can try index.repeat with groupby+cumcount

    n = 3
    out = d.loc[d.index.repeat(n)]
    out = out.assign(col1=out['col1']+out.groupby("col1").cumcount()
                      .replace(0,'').astype(str)).reset_index(drop=True)
    

    print(out)
    
       col1  col2
    0     a    12
    1    a1    12
    2    a2    12
    3     b    13
    4    b1    13
    5    b2    13
    6     c    14
    7    c1    14
    8    c2    14
    9     d    16
    10   d1    16
    11   d2    16
    

    EDIT:

    For repeating values of col1 later, you can use a helper series as grouper:

    d=pd.DataFrame({"col1":["a","b","a","d"],"col2":[12,13,14,16],
                     "col3":[1,2,3,4]})
    n = 3
    out = d.loc[d.index.repeat(n)]
    out = (out.assign(col1=out['col1']+out.groupby(out['col1'].ne(out['col1'].shift())
                                   .cumsum()).cumcount().replace(0,'').astype(str))
               .reset_index(drop=True))
    print(out)
    
       col1  col2  col3
    0     a    12     1
    1    a1    12     1
    2    a2    12     1
    3     b    13     2
    4    b1    13     2
    5    b2    13     2
    6     a    14     3
    7    a1    14     3
    8    a2    14     3
    9     d    16     4
    10   d1    16     4
    11   d2    16     4
    
    0 讨论(0)
提交回复
热议问题