user308827
user308827

Reputation: 21981

Duplicating pandas dataframe vertically

I have the foll. dataframe:

    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

I would like to duplicate it like so:

    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

I can duplicate it using:

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

However, how do I duplicate so that the season column has past as the duplicated values instead of current

Upvotes: 0

Views: 259

Answers (1)

maxymoo
maxymoo

Reputation: 36545

I think this would be a good case for assign since it allows you to keep your functional programming style (i approve!)

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

Upvotes: 1

Related Questions