Home > Blockchain >  How can I replace a certain row in a dataframe with multiple rows that i have stored, using a partic
How can I replace a certain row in a dataframe with multiple rows that i have stored, using a partic

Time:01-10

I am working with the following pandas dataframe:

  time            market_state 
 5:00:00              open  
 6:00:00           continuous
 7:30:00           continuous
 9:12:00           unscheduled
 10:02:02          intraday 
 10:05:03          intraday 
 11:00:33          closed 

My python code removes all rows that have consecutive market_states, like intraday and continuous in the above example.

So i want to store the two rows with market_state intraday, and this is what I use:

store_intraday_market_state = df.loc[df['market_state'] == 'intraday']

So store_intraday_market_state looks like this:

  time            market_state 
 10:02:02          intraday 
 10:05:03          intraday 

The input above (after some python code) becomes:

  time            market_state 
 5:00:00             open  
 7:30:00           continuous
 9:12:00           unscheduled
 10:02:02          intraday 
 11:00:33          closed 

As mentioned the python code combines all rows with consecutive market_states. But now I want to replace the one tow of intraday with the two lines that I stored store_intraday_market_state.

Is there some code where I can do this replacing? *Using the market_state intraday?

CodePudding user response:

If I undestand the question correctly, once you have extracted:

store_intraday_market_state into another dataframe, you can drop such rows from the original dataframe:

df = df[df['market_state'] != 'intraday']

and then do:

df = pandas.concat([df, store_intraday_market_state])

resulting dataframe should contain all the rows from original dataframe except the ones containing intraday and all rows from the stored dataframe with rows containing intraday.

edit: if you want it sorted, you can then do:

df.sort_values(by='time', ascending=True, inplace=True)

  • Related