python - pandas: for each row in df copy row N times with slight changes -
so have dataframe 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 need duplicate each row n times, adding 1 hour start each time, 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 within pandas?
you use reindex expand dataframe, , timedeltaindex add hours:
import pandas 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
Comments
Post a Comment