1

I have a dataframe, where I want to duplicate rows N times, where N is a value in another row.

For example, if this is my dataframe:

   Company  WEEK_DAYS   SEPTEMBER 15    SEPTEMBER 22   SEPTEMBER 29    value      
0  google    MON-FRI         0                0              5          0.5
1  google       TUE          3                2              0          0.7

so, the columns I want to replicate based on their value are SEPTEMBER 15, SEPTEMBER 22, and SEPTEMBER 29, and the value should be value.

So the final output should be something like that:

     Company  WEEK_DAYS     WEEK       value
0    google    MON-FRI    SEPTEMBER 29  0.5
1    google    MON-FRI    SEPTEMBER 29  0.5
2    google    MON-FRI    SEPTEMBER 29  0.5
3    google    MON-FRI    SEPTEMBER 29  0.5
4    google    MON-FRI    SEPTEMBER 29  0.5   
5    google      TUE      SEPTEMBER 15  0.7 
6    google      TUE      SEPTEMBER 15  0.7 
7    google      TUE      SEPTEMBER 15  0.7
8    google      TUE      SEPTEMBER 22  0.7 
9    google      TUE      SEPTEMBER 22  0.7  

I tried using stack and pivot - but I didn't manage to get the desired output.

Any help will be appreciated!

2 Answers 2

3

You can use:


s = df.set_index(['Company','WEEK_DAYS','value']).stack()
df = (s.loc[s.index.repeat(s)]
       .reset_index()
       .drop(0, axis=1)
       .rename(columns={'level_3':'WEEK'})
       .reindex(columns=['Company','WEEK_DAYS','WEEK','value'])
      )
print (df)
  Company WEEK_DAYS          WEEK  value
0  google   MON-FRI  SEPTEMBER 29    0.5
1  google   MON-FRI  SEPTEMBER 29    0.5
2  google   MON-FRI  SEPTEMBER 29    0.5
3  google   MON-FRI  SEPTEMBER 29    0.5
4  google   MON-FRI  SEPTEMBER 29    0.5
5  google       TUE  SEPTEMBER 15    0.7
6  google       TUE  SEPTEMBER 15    0.7
7  google       TUE  SEPTEMBER 15    0.7
8  google       TUE  SEPTEMBER 22    0.7
9  google       TUE  SEPTEMBER 22    0.7
Sign up to request clarification or add additional context in comments.

Comments

0

You can make use of pandas.DataFrame.from_records function.

I suggest you created a list of values you want to have in your new DataFrame, i.e

records = [('google', 'MON-FRI', 'SEPTEMBER 29', '0.5')]
# assumes only one record for MON-FRI .. you might need to do some handling here otherwise
records *= int(df['SEPTEMBER 29'][df[WEEK_DAYS] == 'MON-FRI'])
labels = ['Company', 'WEEK_DAYS', 'WEEK', 'value']

new_df = pd.DataFrame.from_records(records, columns=labels)

I guess it's not the most elegant solution ever, but should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.