C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DataFrame.shift()If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis. It is also capable of dealing with time-series data. Syntax:DataFrame.shift(periods=1, freq=None, axis=0) Parameters:
ReturnsIt returns a shifted copy of DataFrame. Example1: The below example demonstrates the working of the shift(). import pandas as pd info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18], 'b_data': [26, 37, 41, 35, 45], 'c_data': [22, 19, 11, 25, 16]}) info.shift(periods=2) Output a_data b_data c_data 0 NaN NaN NaN 1 NaN NaN NaN 2 45.0 26.0 22.0 3 28.0 37.0 19.0 4 39.0 41.0 11.0 Example2: The example shows how to fill the missing values in the DataFrame using the fill_value. import pandas as pd info= pd.DataFrame({'a_data': [45, 28, 39, 32, 18], 'b_data': [26, 38, 41, 35, 45], 'c_data': [22, 19, 11, 25, 16]}) info.shift(periods=2) info.shift(periods=2,axis=1,fill_value= 70) Output a_data b_data c_data 0 70 70 45 1 70 70 28 2 70 70 39 3 70 70 32 4 70 70 18
Next TopicDataFrame.sort()
|