{"id":9179,"date":"2023-11-07T18:32:22","date_gmt":"2023-11-07T13:02:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=9179"},"modified":"2023-11-10T12:21:33","modified_gmt":"2023-11-10T06:51:33","slug":"python-add-a-column-to-an-existing-csv-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-add-a-column-to-an-existing-csv-file\/","title":{"rendered":"Python: Add a Column to an Existing CSV File"},"content":{"rendered":"

Methods to add a column to an existing CSV File<\/h2>\n

In this article, we will discuss how to add a column to an existing CSV file using csv.reader<\/code>\u00a0and\u00a0csv.DictWriter<\/code>\u00a0 classes. Apart from appending the columns, we will also discuss how to insert columns in between other columns of the existing CSV file.<\/span><\/p>\n

Original CSV file content<\/h3>\n
\n
\n
\n
\n
\n\n\n\n\n\n\n\n\n\n
<\/th>\ntotal_bill<\/th>\ntip<\/th>\nsex<\/th>\nsmoker<\/th>\nday<\/th>\ntime<\/th>\nsize<\/th>\n<\/tr>\n<\/thead>\n
0<\/th>\n16.99<\/td>\n1.01<\/td>\nFemale<\/td>\nNo<\/td>\nSun<\/td>\nDinner<\/td>\n2<\/td>\n<\/tr>\n
1<\/th>\n10.34<\/td>\n1.66<\/td>\nMale<\/td>\nNo<\/td>\nSun<\/td>\nDinner<\/td>\n3<\/td>\n<\/tr>\n
2<\/th>\n21.01<\/td>\n3.50<\/td>\nMale<\/td>\nNo<\/td>\nSun<\/td>\nDinner<\/td>\n3<\/td>\n<\/tr>\n
3<\/th>\n23.68<\/td>\n3.31<\/td>\nMale<\/td>\nNo<\/td>\nSun<\/td>\nDinner<\/td>\n2<\/td>\n<\/tr>\n
4<\/th>\n24.59<\/td>\n3.61<\/td>\nFemale<\/td>\nNo<\/td>\nSun<\/td>\nDinner<\/td>\n4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n