python - How to update the value of one record based on another record with specific criteria in the same dataframes -
in python, have dataframes table of gdp records this
quarter vaule percentage 2017q1-q4 100 18% 2017q1-q3 60 20% 2017q1-q2 30 15% 2017q1-q1 10 10% 2016q1-q4 10 28% 2016q1-q3 6 50% 2016q1-q2 3 45% 2016q1-q1 1 20%
i want output this:
quarter vaule percentage 2017q4 40 18% 2017q3 30 20% 2017q2 20 15% 2017q1 10 10% 2016q4 4 28% 2016q3 3 50% 2016q2 2 45% 2016q1 1 20%
that is, value updated based on computing of other records percentage keeps unchanged.
are there efficient way deal case. thanks!
df.iloc[:-1, 1] = df['vaule'].diff(-1)[:-1] >>> df quarter vaule percentage 0 2017q1-q4 40 18% 1 2017q1-q3 30 20% 2 2017q1-q2 20 15% 3 2017q1-q1 0 10% 4 2016q1-q4 4 28% 5 2016q1-q3 3 50% 6 2016q1-q2 2 45% 7 2016q1-q1 1 20%
Comments
Post a Comment