pandas常用操作

时间处理

pandas._libs.tslibs.timestamps.Timestamp转datetime

from pandas._libs.tslibs.timestamps import Timestamp

ts = Timestamp("2021-11-11 10:11:34", freq='MS')
print(type(ts))
date_time = ts.to_pydatetime()
print(type(date_time))
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<class 'datetime.datetime'>

两行之间相减

df = pd.DataFrame({"old": [10, 30, 20, 50, 40, 40]})
df['up-down'] = df['old'].diff(-1)
df['down-up'] = df['old'].diff(1)
print(df)
   old  up-down  down-up
0   10    -20.0      NaN
1   30     10.0     20.0
2   20    -30.0    -10.0
3   50     10.0     30.0
4   40      0.0    -10.0
5   40      NaN      0.0

你可能感兴趣的:(Python,python,开发语言,后端)