So I have a DataFrame like this:
N start
1 1 08/01/2014 9:30:02
2 1 08/01/2014 10:30:02
3 2 08/01/2014 12:30:02
4 3 08/01/2014 4:30:02
and I need to duplicate each row N times, adding one hour to start each time, like this:
N start
1 1 08/01/2014 9:30:02
2 1 08/01/2014 10:30:02
3 2 08/01/2014 12:30:02
3 2 08/01/2014 13:30:02
4 3 08/01/2014 4:30:02
4 3 08/01/2014 5:30:02
4 3 08/01/2014 6:30:02
how can I do it within pandas?
You could use reindex to expand the DataFrame, and TimedeltaIndex to add the hours:
import pandas as pd
df = pd.DataFrame({'N': [1, 1, 2, 3],
'start': ['08/01/2014 9:30:02',
'08/01/2014 10:30:02',
'08/01/2014 12:30:02',
'08/01/2014 4:30:02']})
df['start'] = pd.to_datetime(df['start'])
df = df.reindex(np.repeat(df.index.values, df['N']), method='ffill')
df['start'] += pd.TimedeltaIndex(df.groupby(level=0).cumcount(), unit='h')
which yields
N start
0 1 2014-08-01 09:30:02
1 1 2014-08-01 10:30:02
2 2 2014-08-01 12:30:02
2 2 2014-08-01 13:30:02
3 3 2014-08-01 04:30:02
3 3 2014-08-01 05:30:02
3 3 2014-08-01 06:30:02
This may not be the most efficient way but will get you the results:
import pandas as pd
l = []
for index,item in df.iterrows():
l.append([item[0],pd.to_datetime(item[1])])
i=1
# it was not clear if you want to repeat based on N or the index... if index then replace item[0] with index
while i<item[0]:
l.append([item[0],pd.to_datetime(item[1])+pd.Timedelta('1 hours')])
i=i+1
dfResult = pd.DataFrame(l,columns=['N','Start'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With