How pandas uses the DataFrame.shift () function
This article will explain in detail how pandas uses the DataFrame.shift () function. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Operation
The pandas DataFrame.shift () function can move data by a specified number of digits
The period parameter specifies the stride of movement. You can specify the axis of movement for positive or negative .axis, with 1 as the row and 0 as the column.
Eg: there is such a DataFrame data: import pandas as pddata1 = pd.DataFrame ({'averse: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],' baked: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]}) print data1 a b0091 1 82 2 73 3 64 4 55 6 37 28 8 19 9 0
If you want to move the data of an and b down one bit:
Data2 = data1.shift (axis=0) print data2 a b0 NaN NaN1 0.0 9.02 1.0 8.03 2.0 7.04 3.0 6.05 4.0 5.06 5.0 4.07 6.0 3.08 7.0 2.09 8.0 1.0
If you move one bit to the right in the line:
Data3 = data1.shift (axis=1) print data3 a b0 NaN 0.01 NaN 1.02 NaN 2.03 NaN 3.04 NaN 4.05 NaN 5.06 NaN 6.07 NaN 7.08 NaN 8.09 NaN 9.0
If you want to move up or left, you can specify (periods=-1):
Data4 = data1.shift (periods=-1, axis=0) print data4 a b0 1.0 8.01 2.0 7.02 3.0 6.03 4.0 5.04 5.0 4.05 6.0 3.06 7.0 2.07 8.0 1.08 9.0 0.09 NaN NaN an example:
Here is a set of data on the total number of people entering and leaving a station for each hour:
Entries_and_exits = pd.DataFrame ({'ENTRIESn': [3144312, 3144335, 3144353, 3144424, 3144594, 3144808,3144895,3144905,3144941,3145094],' EXITSn': [1088151,1088159,108817,1088231, 1088275,1088317,1088328,1088331, 1088420,1088753]})
It is required to calculate the number of people entering and leaving the station every hour.
Idea: the total number of people in the n + 1 hour-the total number of people in the n hour, is the number of people entering and leaving the station in this hour.
Entries_and_exits_hourly = entries_and_exits-entries_and_exits.shift (axis=0) print (entries_and_exits_hourly.fillna (0)) # finally fill NaN ENTRIESn EXITSn0 0.0 0.01 23.0 8.02 18.0 18.03 71.0 54.04 170.0 44.05 214.0 42.06 87.0 11.07 10.0 3.08 36.0 89.09 153.0 333.0 this is the end of the article on how pandas uses the DataFrame.shift () function. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.