I have a dataframe
that looks like this:
year | month | valueCounts |
---|---|---|
2019 | 1 | 73.411285 |
2019 | 2 | 53.589128 |
2019 | 3 | 71.103842 |
2019 | 4 | 79.528084 |
I want valueCounts
column's values to be rolled like:
year | month | valueCounts |
---|---|---|
2019 | 1 | 53.589128 |
2019 | 2 | 71.103842 |
2019 | 3 | 79.528084 |
2019 | 4 | NaN |
I can do this by dropping first index of dataframe
and assigning last index to NaN but it doesn't look efficient. Is there any simpler method to do this?
Thanks.
CodePudding user response:
Assuming your dataframe are already sorted.
Use shift
:
df['valueCounts'] = df['valueCounts'].shift(-1)
print(df)
# Output
year month valueCounts
0 2019 1 53.589128
1 2019 2 71.103842
2 2019 3 79.528084
3 2019 4 NaN