我正在尝试“递归”计算Cat的列值
每个循环都应计算一组x的Cat列最大值(Catz)。如果日期范围小于等于60,则Cat列值应更新为Catz + = 1。我对这一过程进行了一些了解。但是,我之外还有成千上万的其他数据集,无需以arcpy友好格式进行转换。我对熊猫不太熟悉。
参考[1]:Calculate DataFrame values recursively和[2]:python pandas- apply function with two arguments to columns。我仍然不太了解Series / Dataframe概念以及如何应用任一结果
import pandas as pd
import numpy as np
from datetime import datetime
from datetime import datetime as dt
from datetime import timedelta
import time
from datetime import date
dict = {'x':["ASPELBJNMI", "JUNRNEXCRG", "ASPELBJNMI", "JUNRNEXCRG"],
'start': ["6/27/2018", "8/4/2018", "8/22/2018", "8/12/2018"],
'finish':["8/11/2018", "10/3/2018", "8/31/2018", "10/26/2018"],
'DateRange':[0,0,0,0],
'Cat':[-1,-1,-1,-1],
'ID':[1,2,3,4]}
df = pd.DataFrame(dict)
df.set_index('ID')
def classd(houp):
Catz = houp.Cat.min()
Catz +=1
houp = houp.groupby('x')
for x, houp2 in houp:
houp.DateRange = (pd.to_datetime(houp.finish.loc[:]).min()- houp.start.loc[:]).astype('timedelta64[D]')
houp.Cat = np.where(houp.DateRange<=60, Catz , -1)
return houp
df['Cat'] = df[['x','DateRange','Cat']].apply(classd, axis=1).Cat
print df
我运行代码时得到以下回溯
Catz = houp.Cat.min() AttributeError :(““ long”对象没有属性'min'”,u发生在索引0')
期望的结果
OBJECTID_1 * Conc * ID start finish DateRange Cat
1 ASPELBJNMI LAPMT 6/27/2018 8/11/2018 45 0
2 ASPELBJNMI KLKIY 8/22/2018 8/31/2018 9 1
15 JUNRNEXCRG CGCHK 8/4/2018 10/3/2018 60 1
16 JUNRNEXCRG IQYGJ 8/12/2018 10/26/2018 83 -1