[英]How do I use pandas to add rows to a data frame based on a date column and number of days column
I would like to know how to use a start date from a data frame column and have it add rows to the dataframe from the number of days in another column.我想知道如何使用数据框列中的开始日期,并让它从另一列中的天数向 dataframe 添加行。 A new date per day.每天一个新的日期。
Essentially, I am trying to turn this data frame:本质上,我正在尝试转换此数据框:
df = pd.DataFrame({
'Name':['Peter', 'Peter', 'Peter', 'Peter'],
'Planned_Start':['1/1/2019', '1/2/2019', '1/15/2019', '1/2/2019'],
'Duration':[2, 3, 5, 6],
'Hrs':[0.6, 1, 1.2, 0.3]})
...into this data frame: ...进入这个数据框:
df_2 = pd.DataFrame({
'Name':['Peter', 'Peter', 'Peter', 'Peter', 'Peter', 'Peter', 'Peter', 'Peter'],
'Date':['1/1/2019', '1/2/2019', '1/2/2019', '1/3/2019', '1/4/2019','1/10/2019', '1/15/2019', '1/16/2019'],
'Hrs':[0.6, 0.6, 1, 1, 1, 1.2, 0.3, 0.3]})
I'm new to programming in general and have tried the following:我一般是编程新手,并尝试了以下方法:
df_2 = pd.DataFrame({
'date': pd.date_range(
start = df.Planned_Start,
end = pd.to_timedelta(df.Duration, unit='D'),
freq = 'D'
)
})
... and... ... 和...
df["date"] = df.Planned_Start + timedelta(int(df.Duration))
with no luck.没有运气。
I am not entirely sure what you are trying to achieve as your df_2 looks a bit wrong from what I can see.我不完全确定您要实现的目标是什么,因为您的 df_2 从我所看到的看起来有点不对劲。
If you want the take the duration column as days and add this many dates to a Date column, then the below code achieves that:如果您希望将持续时间列作为天数并将这么多日期添加到日期列中,那么下面的代码可以实现:
You can also drop any columns you don't need with pd.Series.drop()
method:您还可以使用pd.Series.drop()
方法删除不需要的任何列:
df = pd.DataFrame({
'Name':['Peter', 'Peter', 'Peter', 'Peter'],
'Planned_Start':['1/1/2019', '1/2/2019', '1/15/2019', '1/2/2019'],
'Duration':[2, 3, 5, 6],
'Hrs':[0.6, 1, 1.2, 0.3]})
df_new = pd.DataFrame()
for i, row in df.iterrows():
for duration in range(row.Duration):
date = pd.Series([pd.datetime.strptime(row.Planned_Start, '%m/%d/%Y') + timedelta(days=duration)], index=['date'])
newrow = row.append(date)
df_new = df_new.append(newrow, ignore_index=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.