我有一个DF,其中包含每月的数据A和B,以获取完整的姓名。 如何使用相同的数据将此DF扩展为每周DF(一周结束)?
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
答案 0 :(得分:1)
首先将“日期”转换为日期时间对象:
date=pd.to_datetime(df['Date'])
df['Date']=date
然后使用“日期”作为索引设置DF:
df=df.set_index(['Date'])
然后将groupby()和resample()与pad()配合使用以进行重新采样:
df.groupby('Name').resample('W-FRI').pad()
这有效!
答案 1 :(得分:0)
尝试:
# align Date to weekends
# if Date is already index, reset index first
df.Date = df.Date + pd.to_timedelta(6-df.Date.dt.dayofweek, unit='d')
# set_index
df.set_index(['Date', 'Name'], inplace=True)
# groupby and interpolate
# drop `interpolate()` if you want to fill the new dates with something else
df.groupby(level=1).apply(lambda x: x.reset_index(level=1)
.drop('Name', axis=1).asfreq('W')
.interpolate())
输出:
+-------+------------+-----------+-----------+
| | | A | B |
+-------+------------+-----------+-----------+
| name | date | | |
+-------+------------+-----------+-----------+
| henry | 2019-03-03 | -0.775991 | -0.370060 |
| | 2019-03-10 | -0.583608 | 0.001735 |
| | 2019-03-17 | -0.391225 | 0.373531 |
| | 2019-03-24 | -0.198842 | 0.745326 |
| | 2019-03-31 | -0.006460 | 1.117122 |
| jack | 2019-03-03 | 0.529327 | -0.317801 |
| | 2019-03-10 | -0.011261 | 0.336492 |
| | 2019-03-17 | -0.551850 | 0.990785 |
| | 2019-03-24 | -1.092438 | 1.645078 |
| | 2019-03-31 | -1.633027 | 2.299371 |
| phil | 2019-03-03 | 0.319571 | 0.282131 |
| | 2019-03-10 | 0.190124 | 0.027607 |
| | 2019-03-17 | 0.060676 | -0.226918 |
| | 2019-03-24 | -0.068771 | -0.481442 |
| | 2019-03-31 | -0.198218 | -0.735966 |
+-------+------------+-----------+-----------+