如何用 pandas 中的重复数据填写行?

问题描述:

在R中,当将不等长的新数据添加到数据帧时,值将重复以填充数据帧:

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 <- c(1,2,3)

收益:

  first second
1     1      1
2     2      2
3     3      3
4     4      1
5     5      2
6     6      3

但是,熊猫需要相等的索引长度。

However, pandas requires equal index lengths.

如何像在R中那样在熊猫中填充重复数据?

How do I "fill in" repeating data in pandas like I can in R?

似乎没有优雅的方法。这是我刚想出的解决方法。基本上创建一个比原始数据框大的重复列表,然后将其左联接。

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']))