【发布时间】:2021-12-10 13:05:12
【问题描述】:
toto =
| C1Date | C1Type | C2Date | C2Type | ..... | C10Date | CType10 | PolDate |
|---|---|---|---|---|---|---|---|
| dd-mm-yyyy | :Proposer | NaT | NaN | NaT | NaN | dd-mm-yyyy | |
| dd-mm-yyyy | :Proposer | NaT | NaN | NaT | NaN | dd-mm-yyyy | |
| dd-mm-yyyy | :Other | dd-mm-yyyy | Proposer | NaT | NaN | dd-mm-yyyy | |
| dd-mm-yyyy | :Proposer | NaT | NaN | NaT | NaN | dd-mm-yyyy | |
| dd-mm-yyyy | :Other | dd-mm-yyyy | Other | NaT | NaN | dd-mm-yyyy |
其中C 指的是Claim
等等。即连续最多有 10 个Claims。
我需要确定是否有任何 Claims 来自 Proposer,并且对于这些声明,它们是否在 PolDate 之后的 3 年内发生(PolDate 总是大于任何 Cdate)
我能够执行以下操作,但无法让日期减法在循环内工作:
CLM = {}
for i in range(1 , 11):
CLM[i] = toto.loc[toto[f'C{i}Type'] == 'Proposer']
#can't get this date subtraction to work within the loop. But can do the subtraction outside of the loop.
CLM[i]['diff'] = (CLM[i]['PolDate'].sub(CLM[i][f'C{i}Date'],
axis=0)).dt.days
use_cols = ['CustomerID', f'C{i}Type', f'C{1}Date', 'PolDate ']
CLM[i] = CLM[i][use_cols]
print("Claim:" + f'{i}' +" "+ str(CLM[i].shape))
错误:
试图在 DataFrame 中的切片副本上设置值。尝试 使用 .loc[row_indexer,col_indexer] = value 代替
此外,无法进行 3 年比较:
if (CLM[1]['diff'] > 1095):
#1095 = (365 * 3):
CLM[1]['CLMLAST3'] = 0
else:
CLM[1]['diff'] = 1
错误:
ValueError:Series 的真值不明确。使用 a.empty, a.bool()、a.item()、a.any() 或 a.all()。
【问题讨论】:
-
使用整齐的数据,你会发现数据分析更容易r4ds.had.co.nz/tidy-data.html
标签: python pandas datetime logical-operators