如何使用日期范围作为列中的值创建数据框?

Suk*_*djf 3 python date dataframe pandas pandasql

我有三个变量

csiti - 23454 : (整数)

单位 - [ 11,22,33,44,55,66,77] : (整数列表,总是特定长度 'n' )

begin_date - '2019-10-16' :(字符串)

我如何从这些数据创建一个数据框,比如

csiti     units forecast_date
1928422     11    2019-10-16  
1928422     22    2019-10-17  
1928422     33    2019-10-18  
1928422     44    2019-10-19  
1928422     55    2019-10-20  
1928422     66    2019-10-21  
1928422     77    2019-10-22  
Run Code Online (Sandbox Code Playgroud)

forecast_date列应该是从begin_date值开始的未来日期。

jez*_*ael 6

使用DataFrame构造函数 with date_rangefor datetime with period 参数按列表中的值的长度units

csiti = 23454
units = [11,22,33,44,55,66,77]
begin_date = '2019-10-16'

df = pd.DataFrame({'csiti':csiti, 
                   'units':units,
                   'forecast_date':pd.date_range(begin_date, periods=len(units))})
print (df.head(10))
   csiti  units forecast_date
0  23454     11    2019-10-16
1  23454     22    2019-10-17
2  23454     33    2019-10-18
3  23454     44    2019-10-19
4  23454     55    2019-10-20
5  23454     66    2019-10-21
6  23454     77    2019-10-22
Run Code Online (Sandbox Code Playgroud)

  • 我认为 OP 希望每个单位都有一个未来的 Forecast_date。但我可能是错的。 (2认同)