Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据框中查找前一天并将其值指定给当前天_Python_Pandas_Dataframe_Group By - Fatal编程技术网

Python 在数据框中查找前一天并将其值指定给当前天

Python 在数据框中查找前一天并将其值指定给当前天,python,pandas,dataframe,group-by,Python,Pandas,Dataframe,Group By,我有一个数据框,它的值与日期对应。现在,我需要获取前一天的列值并将其分配给当前一天 Ex:-当前日期为2020年1月10日,然后数据框中的前一天为2020年1月7日,获取前一天的值并分配给当前 样本输入 样本输出 在日期上使用groupby并使用last聚合te数据帧,然后使用reindex根据原始数据场中的日期将聚合数据帧与索引一致: df1 = df.groupby('Date').last().shift().reindex(df['Date']).reset_index() 结果: p

我有一个数据框,它的值与日期对应。现在,我需要获取前一天的列值并将其分配给当前一天

Ex:-当前日期为2020年1月10日,然后数据框中的前一天为2020年1月7日,获取前一天的值并分配给当前

样本输入 样本输出
日期上使用
groupby
并使用
last
聚合te数据帧,然后使用
reindex
根据原始数据场中的日期将聚合数据帧与索引一致:

df1 = df.groupby('Date').last().shift().reindex(df['Date']).reset_index()
结果:

print(df1)

        Date   Sales  zone_Total
0   2/5/2019     NaN         NaN
1   2/5/2019     NaN         NaN
2   2/5/2019     NaN         NaN
3   2/5/2019     NaN         NaN
4   2/5/2019     NaN         NaN
5   3/5/2019  277.08      358.40
6   3/5/2019  277.08      358.40
7   3/5/2019  277.08      358.40
8   3/5/2019  277.08      358.40
9   6/5/2019  567.95      320.68
10  6/5/2019  567.95      320.68
11  6/5/2019  567.95      320.68
12  6/5/2019  567.95      320.68
13  6/5/2019  567.95      320.68
14  6/5/2019  567.95      320.68

日期上使用
groupby
并使用
last
聚合te数据帧,然后使用
reindex
根据原始数据场中的日期将聚合数据帧与索引一致:

df1 = df.groupby('Date').last().shift().reindex(df['Date']).reset_index()
结果:

print(df1)

        Date   Sales  zone_Total
0   2/5/2019     NaN         NaN
1   2/5/2019     NaN         NaN
2   2/5/2019     NaN         NaN
3   2/5/2019     NaN         NaN
4   2/5/2019     NaN         NaN
5   3/5/2019  277.08      358.40
6   3/5/2019  277.08      358.40
7   3/5/2019  277.08      358.40
8   3/5/2019  277.08      358.40
9   6/5/2019  567.95      320.68
10  6/5/2019  567.95      320.68
11  6/5/2019  567.95      320.68
12  6/5/2019  567.95      320.68
13  6/5/2019  567.95      320.68
14  6/5/2019  567.95      320.68

你好欢迎来到SO。它被认为是一种良好的实践,包括您试图实现期望输出的内容和不起作用的内容。您好。欢迎来到SO。它被认为是一种良好的实践,包括您试图实现期望输出的内容和不起作用的内容。