Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

If the data look like:

Store,Dept,Date,Weekly_Sales,IsHoliday

1,1,2010-02-05,24924.5,FALSE

1,1,2010-02-12,46039.49,TRUE

1,1,2010-02-19,41595.55,FALSE

1,1,2010-02-26,19403.54,FALSE

1,1,2010-03-05,21827.9,FALSE

1,1,2010-03-12,21043.39,FALSE

1,1,2010-03-19,22136.64,FALSE

1,1,2010-03-26,26229.21,FALSE

1,1,2010-04-02,57258.43,FALSE

And I wanna duplicate rows with IsHoliday equal to TRUE, I can do:

is_hol = df['IsHoliday'] == True

df_try = df[is_hol]

df=df.append(df_try*10)

But is there a better way to do this as I need to duplicate holiday rows by 5 times, and I have to append 5 times if using above way.

1 Answer

0 votes
by (41.4k points)

Use df_try inside a list :

>>> df.append([df_try]*5,ignore_index=True)

    Store  Dept     Date Weekly_Sales IsHoliday

0       1 1 2010-02-05      24924.50 False

1       1 1 2010-02-12      46039.49 True

2       1 1 2010-02-19      41595.55 False

3       1 1 2010-02-26      19403.54 False

4       1 1 2010-03-05      21827.90 False

5       1 1 2010-03-12      21043.39 False

6       1 1 2010-03-19      22136.64 False

7       1 1 2010-03-26      26229.21 False

8       1 1 2010-04-02      57258.43 False

9       1 1 2010-02-12      46039.49 True

10      1 1 2010-02-12      46039.49 True

11      1 1 2010-02-12      46039.49 True

12      1 1 2010-02-12      46039.49 True

13      1 1 2010-02-12      46039.49 True

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...