首页 > 解决方案 > 偏移现有日期值,其中值存在于数据框中的另一列中

问题描述

我正在尝试根据另一列(类型)中的值来抵消现有日期(在本例中为 2 个月)。

df.loc[df['Type'] == 'Lock', 'Start'] = df['Start'] + pd.DateOffset(months=-2)

Error: ValueError: cannot reindex from a duplicate axis

这可以像我尝试过的那样在一行中完成吗?如果没有,还有哪些其他方法?

标签: pythonpython-3.xpandas

解决方案


df['Start'] = np.where(df['Type'] == 'Lock',
                        df['Start'] + pd.DateOffset(months=-2),
                        df['Start'])

推荐阅读