如何复制/复制 pandas 数据框行
How to replicate/ copy pandas dataframe rows
我需要在该块下方精确复制同一行块 3 次。就像下面的例子。
DataFrame (df1):-
S.No Cal Date DFM
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
结果:-
第一个数据帧:-
S.No Cal Date DFM
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
一种方法是按 "S.No"
分组,然后按 return 串联帧并在末尾重置索引:
new_df = df.groupby("S.No").apply(lambda d: pd.concat([d]*3)).reset_index(drop=True)
得到
>>> new_df
S.No Cal Date DFM
0 1 8 01-03-2013 100.00%
1 1 7 01-06-2013 100.00%
2 1 6 01-09-2013 100.00%
3 1 5 01-12-2013 99.99%
4 1 4 01-03-2014 99.97%
.. ... ... ... ...
103 3 1 01-12-2014 54.77%
104 3 0 01-03-2015 0.00%
105 3 0 01-06-2015 0.00%
106 3 0 01-09-2015 0.00%
107 3 0 01-12-2015 0.00%
[108 rows x 4 columns]
我需要在该块下方精确复制同一行块 3 次。就像下面的例子。
DataFrame (df1):-
S.No Cal Date DFM
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
结果:-
第一个数据帧:-
S.No Cal Date DFM
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
1 8 01-03-2013 100.00%
1 7 01-06-2013 100.00%
1 6 01-09-2013 100.00%
1 5 01-12-2013 99.99%
1 4 01-03-2014 99.97%
1 3 01-06-2014 99.95%
1 2 01-09-2014 98.89%
1 1 01-12-2014 39.36%
1 0 01-03-2015 0.00%
1 0 01-06-2015 0.00%
1 0 01-09-2015 0.00%
1 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
2 8 01-03-2013 100.00%
2 7 01-06-2013 100.00%
2 6 01-09-2013 100.00%
2 5 01-12-2013 99.99%
2 4 01-03-2014 99.97%
2 3 01-06-2014 97.95%
2 2 01-09-2014 94.42%
2 1 01-12-2014 43.13%
2 0 01-03-2015 0.00%
2 0 01-06-2015 0.00%
2 0 01-09-2015 0.00%
2 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
3 8 01-03-2013 100.00%
3 7 01-06-2013 100.00%
3 6 01-09-2013 100.00%
3 5 01-12-2013 99.99%
3 4 01-03-2014 99.96%
3 3 01-06-2014 99.94%
3 2 01-09-2014 98.85%
3 1 01-12-2014 54.77%
3 0 01-03-2015 0.00%
3 0 01-06-2015 0.00%
3 0 01-09-2015 0.00%
3 0 01-12-2015 0.00%
一种方法是按 "S.No"
分组,然后按 return 串联帧并在末尾重置索引:
new_df = df.groupby("S.No").apply(lambda d: pd.concat([d]*3)).reset_index(drop=True)
得到
>>> new_df
S.No Cal Date DFM
0 1 8 01-03-2013 100.00%
1 1 7 01-06-2013 100.00%
2 1 6 01-09-2013 100.00%
3 1 5 01-12-2013 99.99%
4 1 4 01-03-2014 99.97%
.. ... ... ... ...
103 3 1 01-12-2014 54.77%
104 3 0 01-03-2015 0.00%
105 3 0 01-06-2015 0.00%
106 3 0 01-09-2015 0.00%
107 3 0 01-12-2015 0.00%
[108 rows x 4 columns]