我有一个这样的数据框:
zip season season_start_date season_end_date
zip1 winter 2015-11-25 2016-03-09
我需要一种将开始日期和结束日期之间的日期展平的方法。 我希望这样的输出:
zip season date
zip1 winter 2015-11-25
zip1 winter 2015-11-26
.
.
zip1 winter 2016-03-09
我怎么能更优雅地实现?
data = {"zip":["zip1","zip1"],
"season":["s6","s6"],
"season_start_date": ["2011-01-01","2011-01-01"],
"season_end_date" : ["2012-01-05","2012-01-05"]
}
df = pd.DataFrame(data=data)
谢谢。
答案 0 :(得分:0)
from datetime import datetime, timedelta
Row_to_split = df.loc[1]
Season = Row_to_split['season']
Start_Date = datetime.strptime(Row_to_split['season_start_date']', '%Y-%m-%d')
End_Date = datetime.strptime(Row_to_split['season_end_date']', '%Y-%m-%d')
# initialize new_df
for i in range((End_Date - Start_Date).days+1):
new_df.loc[i] = [season, (Start_Date+timedelta(i)).strftime('%Y-%m-%d')]
那是您想要的吗?我不确定zip列是否为索引,但是如何插入它应该很明显。
答案 1 :(得分:0)
您将需要从每一行生成一个DataFrame,然后将它们连接在一起:
julia> Vector{Float64}.(JSON.parse(trip))
3-element Array{Array{Float64,1},1}:
[-8.7, 6.4]
[7.4, 9.7]
[3.4, 2.1]