2016-02-29 129 views
0

我有這個foll。據幀:垂直複製熊貓數據幀

Month Day season 
0  4 15 current 
1  4 16 current 
2  4 17 current 
3  4 18 current 
4  4 19 current 
5  4 20 current 

我想複製它,像這樣:

Month Day season 
0  4 15 current 
1  4 16 current 
2  4 17 current 
3  4 18 current 
4  4 19 current 
5  4 20 current 
6  4 15 past 
7  4 16 past 
8  4 17 past 
9  4 18 past 
10  4 19 past 
11  4 20 past 

我可以用它複製:

df.append([df]*2,ignore_index=True) 

但是,我怎麼複製,使得season列有past作爲重複值而不是current

回答

1

我認爲這將是assign的好例子,因爲它允許你保持你的函數式編程風格(我贊同!)

In [144]: df.append([df.assign(season='past')]*2,ignore_index=True) 
Out[144]: 
    Month Day season 
0  4 15 current 
1  4 16 current 
2  4 17 current 
3  4 18 current 
4  4 19 current 
5  4 20 current 
6  4 15  past 
7  4 16  past 
8  4 17  past 
9  4 18  past 
10  4 19  past 
11  4 20  past 
12  4 15  past 
13  4 16  past 
14  4 17  past 
15  4 18  past 
16  4 19  past 
17  4 20  past