I have a CSV containing two types of rows, an observation record and on the row below, an observation value which is related to the observation record above. The record line contains a four-letter code which denotes the type of observation. My goal is to create a new CSV containing only those observation records which match a particular list of codes, along with the associated observation values from the row below.
Example from file
OBSERV\LTRC,CL1,0,10.00; OBVAL\14,,0,V; OBSERV\LTRC,CL1,10,20.00; OBVAL\14,,0,V; OBSERV\LTRC,CL1,20,30.00; OBVAL\14,,0,V; OBSERV\LTRC,CL1,30,40.00; OBVAL\14,,0.5,V;
Code so far
import pandas as pd data = pd.read_csv(r"CSVFILEPATH") df = pd.DataFrame(data) df.set_index newdf = df.loc[df.str.contains('LLRT|LLTX|LRRT|LTRC|LV10|LV3|LEDR|LTRV|LES2|LES1', regex = True)] # this returns all observation record rows I care about but I still need the associated observation values. keep_ind =  #This list will contain all indexes to keep observ_ind = ndf.index.values.tolist()#The list of observation record indexes to keep keep_ind.append(observ_ind)#Added these to the keep_ind list
How do I take this list of indexes (keep_ind), append a new list which is the same list with 1 added to each item (to get all of the observation value rows beneath the records) and create a new dataframe which contains all of the rows at each of these indexes in this combined list?
So far, I've tried:
keep_ind.append(observ_ind + 1 for i in observ_ind)
But this gives the error:
generator object <genexpr> at 0x0000028057C33648>]