apply函数
import pandas as pd
import datetime as dt
def get_interval_days(arrLike, start, end): # 【arrLike为一行即axis=1时的row】
start_date = dt.datetime.strptime(arrLike[start], '%Y-%m-%d')
end_date = dt.datetime.strptime(arrLike[end], '%Y-%m-%d')
return (end_date - start_date).days
wbs = {
"wbs": ["job1", "job2", "job3", "job4"],
"date_from": ["2019-04-01", "2019-04-07", "2019-05-16","2019-05-20"],
"date_to": ["2019-05-01", "2019-05-17", "2019-05-31", "2019-06-11"]
}
df = pd.DataFrame(wbs)
df['elapsed'] = df.apply(
get_interval_days, axis=1, args=('date_from', 'date_to')) # 【这里注意会默认传递一个函数即一行作为参数,注意官方文档说明】
Note: 查api时,注意是Series的方法还是dataframe的方法
reference:
https://blog.csdn.net/stone0823/article/details/100008619
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html