1
目标:对于在接下来的9个月内具有日期的特定列中的单元格,将新列添加到Master,选择“Yes”,日期即将到来,或者“No”,因为日期不是接下来。使用熊猫,添加timedelta到主数据框中的新行?
我是熊猫新手,所以任何帮助都会很棒。我有一个数据框称为“Master”,因为我将所有内容添加到数据框中,然后导出到Excel电子表格。使用下面的代码,我为日期创建了一个新的数据框,并试图说“如果单元格处于日期状态,请在新列中输入”是“。
我怀疑我的问题是,我试图把熊猫当作数组来处理,但是它实际上做的是在创建一个新的数据框时创建全新的索引并且不保留它如何映射到主。我的理解是否正确?如果是这样,我怎样才能以更优雅的方式实现我的目标?
def mess_with_time(Master):
now = datetime.datetime.now()
nine_months_in_the_future = str(datetime.datetime.today() + relativedelta(months=+8))
just_future_month=int(nine_months_in_the_future[5:7])
just_future_year=int(nine_months_in_the_future[0:4])
dates = Master[(Master['Contract End Date'] > '{}-{}-1'.format(now.year,now.month)) & (Master['Contract End Date'] <= '{}-{}-20'.format(just_future_year, just_future_month))]
Master['Contract End date coming up?'] = np.where(dates, 'Yes', 'No') #this breaks with
the error 'Length of values does not match length of index'
def Create_Master(file):
df = pd.ExcelFile('{}.xlsx'.format(file))
Master = df.parse('Main').fillna(0)