Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: for each row in df copy row N times with slight changes

Tags:

python

pandas

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?

like image 359
Philipp_Kats Avatar asked Aug 16 '15 18:08

Philipp_Kats


2 Answers

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
like image 159
unutbu Avatar answered Oct 07 '22 14:10

unutbu


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'])
like image 33
Shahram Avatar answered Oct 07 '22 12:10

Shahram