I am looking to insert a row into a dataframe between two existing rows based on certain criteria.
For example, my data frame:
import pandas as pd
df = pd.DataFrame({'Col1':['A','B','D','E'],'Col2':['B', 'C', 'E', 'F'], 'Col3':['1', '1', '1', '1']})
Which looks like:
Col1 Col2 Col3
0 A B 1
1 B C 1
2 D E 1
3 E F 1
I want to be able to insert a new row between Index 1 and Index 2 given the condition:
n = 0
while n < len(df):
(df.ix[n]['Col2'] == df.ix[n+1]['Col1']) == False
Something, Something, insert row
n+=1
My desired output table would look like:
Col1 Col2 Col3
0 A B 1
1 B C 1
2 C D 1
3 D E 1
4 E F 1
I am struggling with conditional inserting of rows based on values in the previous and proceeding records. I ultimately want to preform the above exercise on my real world example which would include multiple conditions, and preserving the values of more than one column (in this example it was Col3, but in my real world it would be multiple columns)