Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
dataframe.pct_change() function calculates the percentage change between the current and a prior element. This function by default calculates the percentage change from the immediately previous row.
Note : This function is mostly useful in the time-series data.
Syntax: DataFrame.pct_change(periods=1, fill_method=’pad’, limit=None, freq=None, **kwargs)
periods : Periods to shift for forming percent change.
fill_method : How to handle NAs before computing percent changes.
limit : The number of consecutive NAs to fill before stopping
freq : Increment to use from time series API (e.g. ‘M’ or BDay()).
**kwargs : Additional keyword arguments are passed into DataFrame.shift or Series.shift.
Returns : The same type as the calling object.
Example #1: Use
pct_change() function to find the percentage change in the time-series data.
Let’s use the
dataframe.pct_change() function to find the percent change in the data.
The first row contains
NaN values, as there is no previous row from which we can calculate the change.
Example #2: Use
pct_change() function to find the percentage change in the data which is also having
The first row contains
NaN values, as there is no previous row from which we can calculate the change. All the
NaN values in the dataframe has been filled using
- Python | pandas.map()
- Python | Pandas Series.str.pad()
- Python | Pandas dataframe.mod()
- Python | Pandas Series.lt()
- Python | Pandas dataframe.min()
- Python | Pandas dataframe.sum()
- Python | Pandas dataframe.take()
- Python | Pandas Period.second
- Python | Pandas Series.str.contains()
- Python | Pandas Series.last()
- Python | Pandas dataframe.mul()
- Python | Pandas Timestamp.day
- Python | Pandas dataframe.ne()
- Python | Pandas Series.dt.day
- Python | Pandas Series.all()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.