Python 3.x 在python中使用reset_index()根据日期范围创建其他行
我有一个大型ish数据集,其中包含如下行:Python 3.x 在python中使用reset_index()根据日期范围创建其他行,python-3.x,pandas,indexing,reset,Python 3.x,Pandas,Indexing,Reset,我有一个大型ish数据集,其中包含如下行: RecordID CarName ShopArrive ShopLeave NameOfShop Month Year ShopDays 101 Vehic056 08/22/2019 09/03/2019 Joes 08 2015 13 我想在pandas中使用reset_index()或类似的方法来复制~13条记录,以逐行表示这辆车在商店里呆了13天 py
RecordID CarName ShopArrive ShopLeave NameOfShop Month Year ShopDays
101 Vehic056 08/22/2019 09/03/2019 Joes 08 2015 13
我想在pandas中使用reset_index()或类似的方法来复制~13条记录,以逐行表示这辆车在商店里呆了13天
python新手,熟悉SQL,因此非常感谢您的帮助。df.reset_index()方法不会修改行数。你需要寻找类似的东西
例如,我尝试过这种方法,但在当时得到了一个不受支持的操作数类型错误,即使它在datetime64中作为类型:````````````````````df=df.loc[np.repeat(df.index,df['ShopDapys]]+=pd.to_timedelta(df.groupby(level=0.cumcount(),unit='d')''df=df=df.reset\u index(drop=True)谢谢,里奇耶夫-这非常有用。尽管我能够从上面修复代码(重新构建正确的数组,使其成为datetime64),并且数据帧中的行数已扩展。
df = df.assign(dates_in_shop=
[[row.ShopArrive + i for i in range(row.ShopDays + 1)]
for row in df.itertuples()])
df = df.explode('dates_in_shop').reset_index(drop=True)