0

I have a dataframe below:

Date_time State Time_in_state(Hour)
10/8/2022 6:30:00 State A 1.3
10/8/2022 7:40:00 State B 0.3

I need to inject 2 row at 7:00:00 using Python pd.append function base on the condition below:

Date_time State Time_in_state(Hour)
10/8/2022 6:30:00 State A 1.3
10/8/2022 7:00:00 State A 0.5
10/8/2022 7:00:00 State A 0.667
10/8/2022 7:40:00 State B 0.3

Samilar to :

Date_time State Time_in_state(Hour)
10/8/2022 18:30:00 State A 1.3
10/8/2022 19:00:00 State A 0.5
10/8/2022 19:00:00 State A 0.667
10/8/2022 19:40:00 State B 0.3

Really need your help!

tungage
  • 13
  • 4
  • Does this answer your question? [Is it possible to insert a row at an arbitrary position in a dataframe using pandas?](https://stackoverflow.com/questions/15888648/is-it-possible-to-insert-a-row-at-an-arbitrary-position-in-a-dataframe-using-pan) – Nick ODell Oct 08 '22 at 23:40
  • Thank you for your cmt. There's not specific for the conditions of index. – tungage Oct 10 '22 at 14:39
  • `There's not specific for the conditions of index.` What do you mean by that? – Nick ODell Oct 10 '22 at 15:04
  • I mean in this case, I tried to look at the 'Date_time' column. Whenever the transition from 6:xx:xx to 7:xx:xx or 18:xx:xx to 19:xx:xx It's also insert two row 7:00:00. 'line = DataFrame({"onset": 30.0, "length": 1.3}, index=[3])' you can see this example, It's treat the index to 3 for inserting. – tungage Oct 11 '22 at 14:07
  • So, you're saying you want to insert the rows at a position such that the dataframe stays sorted with respect to that column? You can do that by appending to the end, then sorting it. – Nick ODell Oct 11 '22 at 18:47

0 Answers0