我有一个 pandas 数据框,其中包含与此类似的日期和字符串:
Start End Note Item
2016-10-22 2016-11-05 Z A
2017-02-11 2017-02-25 W B
我需要将其扩展/转换为以下内容,在 Start 和 End 列之间填写周数 (W-SAT),然后在 中填写数据strong>注意和元素:
Start Note Item
2016-10-22 Z A
2016-10-29 Z A
2016-11-05 Z A
2017-02-11 W B
2017-02-18 W B
2017-02-25 W B
使用 Pandas 执行此操作的最佳方法是什么?某种多索引应用?
最佳答案
您可以遍历每一行并创建一个新的数据框,然后将它们连接在一起
pd.concat([pd.DataFrame({'Start': pd.date_range(row.Start, row.End, freq='W-SAT'),
'Note': row.Note,
'Item': row.Item}, columns=['Start', 'Note', 'Item'])
for i, row in df.iterrows()], ignore_index=True)
Start Note Item
0 2016-10-22 Z A
1 2016-10-29 Z A
2 2016-11-05 Z A
3 2017-02-11 W B
4 2017-02-18 W B
5 2017-02-25 W B
关于python - 使用列中的日期范围扩展 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151886/