Home > front end >  Create a pandas data frame and fill relevant values in it from a reference data frame
Create a pandas data frame and fill relevant values in it from a reference data frame

Time:06-08

I have a pandas data frame called df that looks like this:

enter image description here

I need to create a new data frame / modify the existing one so that it will expand the values in df['SOV'] to show all values from 1-80 like this:

enter image description here

The values of SOV = 5 have been backfilled for SOV 1-5 in the new data frame. The values of SOV = 10 have been backfilled for SOV 6-10 in the new data frame and so on.

CodePudding user response:

This should work

# assign ranges to SOV column (from previous row's SOV until current row's SOV)
df['SOV'] = [range(s 1,e 1) for s,e in zip(pd.Series([0]).append(df['SOV']), df['SOV'])]
# explode the ranges (the other columns will be copied across rows)
df = df.explode('SOV')

CodePudding user response:

If there is always necessary generate 5 new rows is possible use Index.repeat by 5, generate new rows by DataFrame.loc and then rewrite column manually:

df = df.loc[df.index.repeat(5)].copy()
df['SOV'] = range(1, len(df)   1)

Or for general solution repeat rows by difference by Series.diffand generate column SOV by counter by GroupBy.cumcount:

df = df.loc[df.index.repeat(df['SOV'].diff().fillna(df['SOV']))].copy()
df['SOV'] -= df.groupby(level=0).cumcount(ascending=False)
  • Related