user11509999
user11509999

Reputation:

Add two dataframe rows?

I have a df with year, I'm trying to combine two rows in a dataframe.

df

        year
    0   2020
    1   2019
    2   2018
    3   2017
    4   2016

Final df

        year     combine
    0   2020     2020-2019
    1   2019     2019-2018
    2   2018     2018-2017
    3   2017     2017-2016
    4   2016     NaN

Upvotes: 0

Views: 58

Answers (1)

BENY
BENY

Reputation: 323316

Let us do shift

df['combine'] = df.year.astype(str) + '-' +  df.year.astype(str).shift(-1)
df
Out[302]: 
   year    combine
0  2020  2020-2019
1  2019  2019-2018
2  2018  2018-2017
3  2017  2017-2016
4  2016        NaN

Upvotes: 2

Related Questions