I am using pandas for read and write csv file. i want to extend column in csv. i am reading csv in chunks, when first chunk read then extend new column data to it and then write that chunk to csv, then receive 2nd chunk add new column data and then write to csv. Below is what i want.
SN, Name, City
1, John, Washington
2, Eric, Los Angeles
3, Brad, Texas
it append the new column data to new rows rather then appending to existing row.
SN, Name,
1, John,
2, Eric,
3, Brad,
4 City
5 Washington
6 Los Angeles
7 Texas
Here is the code.
with open('exampleCsv.csv', 'r') as fieldnames:
csv_input = pd.read_csv(fieldnames, chunksize=4)
list_1 = [5,6,7,8,1,2,3,4]
list_len = len(list_1)//2
for i in csv_input:
if list_len%2 == 0:
if list_iter == 1:
i['channel_name'] = list_1[list_len:]
i.to_csv('exampleCsv.csv', mode='a', index=True)
elif list_iter == 2:
i['channel_name'] = list_1[:list_len]
i.to_csv('exampleCsv.csv', header=None, mode='a', index=True)
elif list_len%2 == 1:
i['channel_name'] = list1
i.to_csv('exampleCsv.csv', header=None, mode='a', index=True)
list_iter = list_iter + 1