如何用 pandas 中的重复数据填写行? [英] How to fill in rows with repeating data in pandas?
本文介绍了如何用 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']))
这篇关于如何用 pandas 中的重复数据填写行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文