Apply function to pandas DataFrame that can return multiple rows

后端 未结 5 1330
轮回少年
轮回少年 2020-12-03 01:37

I am trying to transform DataFrame, such that some of the rows will be replicated a given number of times. For example:

df = pd.DataFrame({\'class\': [\'A\',         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 02:17

    repeated_items = [list(row[1]*row[2]) for row in df.itertuples()]
    

    will create a nested list:

    [['A'], [], ['C', 'C']]
    

    which you can then iterate over with list comprehensions to create a new data frame:

    new_df = pd.DataFrame({"class":[j for i in repeated_items for j in i]})
    

    Of course, you can do it in a single line as well if you want:

    new_df = pd.DataFrame({"class":[j for i in [list(row[1]*row[2]) for row in df.itertuples()] for j in i]})
    

提交回复
热议问题