I have a set of CSVs that I need to modify. The following code finds the places where the modification needs to happen -- where the 'Markers' column has consecutive 4s, 3s, or a 5-3, or a 4-3. I need to insert a 2 in between any of those patterns (i.e. 3,3, should become 3,2,3. 5,3, should become 5,2,3, etc)
The following code finds those patterns by inserting a new copy column of markers, shifted one down:
columns=['TwoThrees','TwoFours', 'FiveThree', 'FourThree']
PVTdfs=[]
def PVTscore(pdframe):
Taskname ='PVT_'
ID=(re.findall('\\d+', file))
dfName = 'Scoringdf_'+str(ID)
dfName = pd.DataFrame([[0,0,0,0]],columns=columns, index=ID)
pdframe['ShiftedMarkers'] = pdframe.Markers.shift()
for index, row in pdframe.iterrows():
if row[1] == row[2]:
if row[1]==3:
print("looks like two threes")
print(index, row[1],row[2])
dfName.TwoThrees[0]+=1
elif row[1]==4:
print("looks like two fours")
print(index, row[1],row[2])
dfName.TwoFours[0]+=1
if row[1]==3 and row[2]==5:
print("looks like a three then a five")
print(index, row[1],row[2])
dfName.FiveThree[0]+=1
if row[1]==3 and row[2]==4:
print("looks like a four then a three")
print(index, row[1],row[2])
dfName.FourThree[0]+=1
if 'post' in file:
print('Looks like a Post')
PrePost = 'Post_'
dfName.columns = [Taskname+ PrePost +x for x in columns]
elif'pre' in file:
print('Looks like a PRE')
PrePost = 'Pre_'
dfName.columns = [Taskname+ PrePost +x for x in columns]
PVTdfs.append(dfName)
an example CSV is:
Relative Time Markers
1 928 1
2 1312 2
3 1364 5
4 3092 2
5 3167 3
6 5072 2
7 5147 3
8 5908 2
9 5969 3
10 7955 3 <-- these two should be amended
11 9560 3 <-- these two should be amended
12 10313 2
13 10391 3
14 11354 2
Desired output:
Relative Time Markers
1 928 1
2 1312 2
3 1364 5
4 3092 2
5 3167 3
6 5072 2
7 5147 3
8 5908 2
9 5969 3
10 NAN 2
11 7955 3 <-- fixed
12 NAN 2
13 9560 3 <-- fixed
14 10313 2
15 10391 3
16 11354 2
I've tried np.insert and df.loc assignments but they just replace the existing row, I need to insert a new one and update the indexing.