Python Pandas 复制数据帧中的行 [英] Python Pandas replicate rows in dataframe
本文介绍了Python Pandas 复制数据帧中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果数据框看起来像:
Store,Dept,Date,Weekly_Sales,IsHoliday1,1,2010-02-05,24924.5,FALSE1,1,2010-02-12,46039.49,TRUE1,1,2010-02-19,41595.55,FALSE1,1,2010-02-26,19403.54,FALSE1,1,2010-03-05,21827.9,FALSE1,1,2010-03-12,21043.39,FALSE1,1,2010-03-19,22136.64,FALSE1,1,2010-03-26,26229.21,FALSE1,1,2010-04-02,57258.43,FALSE
我想复制 IsHoliday
等于 TRUE 的行,我可以这样做:
is_hol = df['IsHoliday'] == Truedf_try = df[is_hol]df=df.append(df_try*10)
但是有没有更好的方法来做到这一点,因为我需要将假日行复制 5 次,如果使用上述方式,我必须附加 5 次.
解决方案
你可以把 df_try
放在一个列表中,然后按照你的想法去做:
If the dataframe looks like:
Store,Dept,Date,Weekly_Sales,IsHoliday
1,1,2010-02-05,24924.5,FALSE
1,1,2010-02-12,46039.49,TRUE
1,1,2010-02-19,41595.55,FALSE
1,1,2010-02-26,19403.54,FALSE
1,1,2010-03-05,21827.9,FALSE
1,1,2010-03-12,21043.39,FALSE
1,1,2010-03-19,22136.64,FALSE
1,1,2010-03-26,26229.21,FALSE
1,1,2010-04-02,57258.43,FALSE
And I wanna duplicate rows with IsHoliday
equal to TRUE, I can do:
is_hol = df['IsHoliday'] == True
df_try = df[is_hol]
df=df.append(df_try*10)
But is there a better way to do this as I need to duplicate holiday rows 5 times, and I have to append 5 times if using the above way.
解决方案
You can put df_try
inside a list and then do what you have in mind:
>>> df.append([df_try]*5,ignore_index=True)
Store Dept Date Weekly_Sales IsHoliday
0 1 1 2010-02-05 24924.50 False
1 1 1 2010-02-12 46039.49 True
2 1 1 2010-02-19 41595.55 False
3 1 1 2010-02-26 19403.54 False
4 1 1 2010-03-05 21827.90 False
5 1 1 2010-03-12 21043.39 False
6 1 1 2010-03-19 22136.64 False
7 1 1 2010-03-26 26229.21 False
8 1 1 2010-04-02 57258.43 False
9 1 1 2010-02-12 46039.49 True
10 1 1 2010-02-12 46039.49 True
11 1 1 2010-02-12 46039.49 True
12 1 1 2010-02-12 46039.49 True
13 1 1 2010-02-12 46039.49 True
这篇关于Python Pandas 复制数据帧中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文