Question: I have the following dataframe with large amounts of data.  C1 C2 C3 C4 t # 0 e 10001 252207 CAT t # 1 e 100018 219559 DOG t # 2 e 100068 251102 CAT t # 3 e 100089 107320 LION t # 4 e 100110 250975 TIGER t # 5 e 100111 28540 TIGER t # 6 e 100112 252253 COW t # 7 e 100157 17883 COW t # 8 e 100158 106226 DOG t # 9 e 100189 32004 CAT e 100189 250979 DOG e 100189 107997 CAT t # 10 e 100190 251325 LION e 100190 251325 LION e 100190 250999 LION t # 11 e 100194 250979 COW e 100194 65072 COW . ... ... ... t # 10000 e 200194 550979 COW e 200194 565072 COW  If there is t # {i} in C2, then the other columns in this row are empty What I would like to achieve is to copy the existing numeric values from C2 and C3 - from one t # {i} to the next t # {i+1} and when C1 has a value of e - and insert them sequentially into C2 above in such a way that they do not repeat and that in C1 for these values there is always the letter v, while in C3 there can be anything different, the most readable would be to assign them, values from 1 upwards. C4 always remains empty. Below the output I would like to achieve.  C1 C2 C3 C4 t # 0 v 10001 1 v 252207 2 e 10001 252207 CAT t # 1 v 100018 1 v 219559 2 e 100018 219559 DOG t # 2 v 100068 1 v 251102 2 e 100068 251102 CAT t # 3 v 100089 1 v 107320 2 e 100089 107320 LION t # 4 v 100110 1 v 250975 2 e 100110 250975 TIGER t # 5 v 100111 1 v 28540 2 e 100111 28540 TIGER t # 6 v 100112 1 v 252253 2 e 100112 252253 COW t # 7 v 100068 1 v 251102 2 e 100157 17883 COW t # 8 v 100158 1 v 106226 2 e 100158 106226 DOG t # 9 v 100189 1 v 32004 2 v 250979 3 v 107997 4 e 100189 32004 CAT e 100189 250979 DOG e 100189 107997 CAT t # 10 v 100190 1 v 251325 2 v 251325 3 v 250999 4 e 100190 251325 LION e 100190 251325 LION e 100190 250999 LION t # 11 v 100194 1 v 250979 2 v 65072 3 e 100194 250979 COW e 100194 65072 COW t # 10000 v 200194 1 v 550979 2 v 565072 3 e 200194 550979 COW e 200194 565072 COW  I absolutely do not know how I can do this, For any help thanks. EDIT: I use txt file in df with open("results123.txt", "r") as f: text = [line.split() for line in f] df = pandas.DataFrame( text, columns=['C1','C2','C3','C4'] )  and my df looks like this: import json d = df.to_dict(orient='records') print(d) [{'C1': 't', 'C2': '#', 'C3': '0', 'C4': None}, {'C1': 'e', 'C2': '10001', 'C3': '252207', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '1', 'C4': None}, {'C1': 'e', 'C2': '100018', 'C3': '219559', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '2', 'C4': None}, {'C1': 'e', 'C2': '100068', 'C3': '251102', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '3', 'C4': None}, {'C1': 'e', 'C2': '100089', 'C3': '107320', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '4', 'C4': None}, {'C1': 'e', 'C2': '100110', 'C3': '250975', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '5', 'C4': None}, {'C1': 'e', 'C2': '100111', 'C3': '28540', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '6', 'C4': None}, {'C1': 'e', 'C2': '100112', 'C3': '252253', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '7', 'C4': None}, {'C1': 'e', 'C2': '100157', 'C3': '17883', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '8', 'C4': None}, {'C1': 'e', 'C2': '100158', 'C3': '106226', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '9', 'C4': None}, {'C1': 'e', 'C2': '100189', 'C3': '32004', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100189', 'C3': '250979', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100189', 'C3': '107997', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '10', 'C4': None}, {'C1': 'e', 'C2': '100190', 'C3': '251325', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100190', 'C3': '251325', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100190', 'C3': '250999', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '11', 'C4': None}, {'C1': 'e', 'C2': '100194', 'C3': '250979', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100194', 'C3': '65072', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '12', 'C4': None}, {'C1': 'e', 'C2': '100203', 'C3': '250979', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '13', 'C4': None}, {'C1': 'e', 'C2': '100224', 'C3': '234727', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '14', 'C4': None}, {'C1': 'e', 'C2': '100229', 'C3': '253351', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '15', 'C4': None}, {'C1': 'e', 'C2': '100230', 'C3': '228074', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100230', 'C3': '182318', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '16', 'C4': None}, {'C1': 'e', 'C2': '100231', 'C3': '252665', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '17', 'C4': None}, {'C1': 'e', 'C2': '100269', 'C3': '41716', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '18', 'C4': None}, {'C1': 'e', 'C2': '100277', 'C3': '251094', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '19', 'C4': None}, {'C1': 'e', 'C2': '100281', 'C3': '253887', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '20', 'C4': None}, {'C1': 'e', 'C2': '100283', 'C3': '20766', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '21', 'C4': None}, {'C1': 'e', 'C2': '100288', 'C3': '251001', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '22', 'C4': None}, {'C1': 'e', 'C2': '10029', 'C3': '250979', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '23', 'C4': None}, {'C1': 'e', 'C2': '100290', 'C3': '197427', 'C4': 'BOVINI'}, {'C1': 'e', 'C2': '100290', 'C3': '41716', 'C4': 'BOVINI'}, {'C1': 't', 'C2': '#', 'C3': '24', 'C4': None}, {'C1': 'e', 'C2': 'ID_NODO_OR', 'C3': 'ID_NODO_DEST', 'C4': 'SPECIE'}, {'C1': 'e', 'C2': 'ID_NODO_OR', 'C3': 'ID_NODO_DEST', 'C4': 'SPECIE'}] 

Answer: You can iterate through the rows and use startswith() on row C2, in addition to the other condition: import pandas as pd def get_rows(df): Vs, res = 1, [] for _, r in df.iterrows(): if r['C2'].startswith('t #'): Vs = 1 elif r['C1'] == 'e': res.append(['v', r['C2'], str(Vs), None]) Vs += 1 res.append([r['C1'], r['C2'], r['C3'], r['C4']]) return pd.DataFrame(res, columns=df.columns) D = [ {"C1": "t", "C2": "#", "C3": "0", "C4": None}, {"C1": "e", "C2": "10001", "C3": "252207", "C4": "BOVINI"}, {"C1": "t", "C2": "#", "C3": "1", "C4": None}, {"C1": "e", "C2": "100018", "C3": "219559", "C4": "BOVINI"}, {"C1": "t", "C2": "#", "C3": "2", "C4": None}, {"C1": "e", "C2": "100068", "C3": "251102", "C4": "BOVINI"} ] print(get_rows(pd.DataFrame(D)))  Prints  C1 C2 C3 C4 0 t # 0 None 1 v 10001 1 None 2 e 10001 252207 BOVINI 3 t # 1 None 4 v 100018 2 None 5 e 100018 219559 BOVINI 6 t # 2 None 7 v 100068 3 None 8 e 100068 251102 BOVINI  Write: you can simply add these lines at the end of function:  res = pd.DataFrame(res, columns=df.columns) res.to_csv(filename, sep='\t', index=False, header=False)