'How to add a new row after every unique entries in pandas dataframe
I have to add a new row at the end of each person information. In the new row which we will add all the information will be same as last row like name
, last_update
, and new date
will have same value as last row. But visited column should always have value abc
and start date will have same value as last_update
and date will have same value as new date.
lets say dataframe looks like this:
Name | last_update | New date | visited | start_date | date |
---|---|---|---|---|---|
Ravi | 22-04-2010 | 22-04-2010 | abc | 22-09-1987 | 24-04-2010 |
Ravi | 27-04-2010 | 28-04-2010 | xyz | 24-07-2001 | 2-08-2015 |
Rajesh | 22-06-2012 | 22-07-2012 | yyy | 12-08-2005 | 25-08-2012 |
Rajesh | 24-02-2014 | 25-04-2014 | zzz | 18-06-2002 | 26-06-2014 |
New dataframe will be
Name | last_update | New date | visited | start_date | date |
---|---|---|---|---|---|
Ravi | 22-04-2010 | 22-04-2010 | abc | 22-09-1987 | 24-04-2010 |
Ravi | 27-04-2010 | 28-04-2010 | xyz | 24-07-2001 | 2-08-2015 |
Ravi | 27-042010 | 28-04-2010 | abc | 27-042010 | 28-04-2010-----newly added row |
Rajesh | 22-06-2012 | 22-07-2012 | yyy | 12-08-2005 | 25-08-2012 |
Rajesh | 24-02-2014 | 25-04-2014 | zzz | 18-06-2002 | 26-06-2014 |
Rajesh | 24-02-2014 | 25-04-2014 | abc | 24-02-2014 | 25-04-2014----newly added row |
Solution 1:[1]
I would suggest you to add one more column with id like conversation_id or sender_id either of it you can choose, and make sure to use it as the primary key. So, now whenever you save the information it will be like the following:
id |Name |last_update |New date |visited |start_date |date
gdast767jd |Ravi |22-04-2010 |22-04-2010 |abc |22-09-1987 |24-04-2010
jagsdjgs01 |Ravi |27-04-2010 |28-04-2010 |xyz |24-07-2001 |2-08-2015
agsdjgs02 |Ravi |27-042010 |28-04-2010 |abc |27-042010 |28-04-2010-----newly added row
jagsdjyuay |Rajesh |22-06-2012 |22-07-2012 |yyy |12-08-2005 |25-08-2012
jag09sad |Rajesh |24-02-2014 |25-04-2014 |zzz |18-06-2002 |26-06-2014
ja17987h |Rajesh |24-02-2014 |25-04-2014 |abc |24-02-2014 |25-04-2014----newly added row
Above ids are imaginary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dishant Gandhi |