{"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
<\/th>\n
total_bill<\/th>\n
tip<\/th>\n
sex<\/th>\n
smoker<\/th>\n
day<\/th>\n
time<\/th>\n
size<\/th>\n<\/tr>\n<\/thead>\n
\n
\n
0<\/th>\n
16.99<\/td>\n
1.01<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n<\/tr>\n
\n
1<\/th>\n
10.34<\/td>\n
1.66<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n<\/tr>\n
\n
2<\/th>\n
21.01<\/td>\n
3.50<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n<\/tr>\n
\n
3<\/th>\n
23.68<\/td>\n
3.31<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n<\/tr>\n
\n
4<\/th>\n
24.59<\/td>\n
3.61<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n
\n
Method 1-Add a column with the same values to an existing CSV file<\/h3>\n<\/li>\n<\/ul>\n
In this, we see how we make one column and add it to our CSV file but all the values in this column are the same.<\/p>\n
Steps will be to append a column in CSV file are,<\/p>\n
\n
Open \u2018input.csv\u2019 file in read mode and create csv.reader object for this CSV file<\/li>\n
Open \u2018output.csv\u2019 file in write mode and create csv.writer object for this CSV file<\/li>\n
Using reader object, read the \u2018input.csv\u2019 file line by line<\/li>\n
For each row (read like a list ), append default text in the list.<\/li>\n
Write this updated list \/ row in the \u2018output.csv\u2019 using csv.writer object for this file.<\/li>\n
Close both input.csv and output.csv file.<\/li>\n<\/ol>\n
Let see this with the help of an example<\/p>\n
from csv import writer\r\nfrom csv import reader\r\ndefault_text = 'New column'\r\n# Open the input_file in read mode and output_file in write mode\r\nwith open('example1.csv', 'r') as read_obj, \\\r\n open('output_1.csv', 'w', newline='') as write_obj:\r\n # Create a csv.reader object from the input file object\r\n csv_reader = reader(read_obj)\r\n # Create a csv.writer object from the output file object\r\n csv_writer = writer(write_obj)\r\n # Read each row of the input csv file as list\r\n for row in csv_reader:\r\n # Append the default text in the row \/ list\r\n row.append(default_text)\r\n # Add the updated row \/ list to the output file\r\n csv_writer.writerow(row)\r\noutput_data=pd.read_csv('output_1.csv')\r\noutput_data.head()<\/pre>\n
Output<\/p>\n
\n\n
\n
<\/th>\n
total_bill<\/th>\n
tip<\/th>\n
sex<\/th>\n
smoker<\/th>\n
day<\/th>\n
time<\/th>\n
size<\/th>\n
New column<\/th>\n<\/tr>\n<\/thead>\n
\n
\n
0<\/th>\n
16.99<\/td>\n
1.01<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n
New column<\/td>\n<\/tr>\n
\n
1<\/th>\n
10.34<\/td>\n
1.66<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n
New column<\/td>\n<\/tr>\n
\n
2<\/th>\n
21.01<\/td>\n
3.50<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n
New column<\/td>\n<\/tr>\n
\n
3<\/th>\n
23.68<\/td>\n
3.31<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n
New column<\/td>\n<\/tr>\n
\n
4<\/th>\n
24.59<\/td>\n
3.61<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
4<\/td>\n
New column<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Here we see that new column is added but all value in this column is same.<\/p>\n
Now we see how we can add different values in the column.<\/p>\n
\n
\n
\u00a0Method 2-Add a column to an existing CSV file, based on values from other columns<\/h3>\n<\/li>\n<\/ul>\n
In this method how we can make a new column but in this column the value we add will be a combination of two or more columns. As we know there is no direct function to achieve so we have to write our own function to achieve this task. Let see the code for this.<\/p>\n
from csv import writer\r\nfrom csv import reader\r\ndef add_column_in_csv(input_file, output_file, transform_row):\r\n \"\"\" Append a column in existing csv using csv.reader \/ csv.writer classes\"\"\"\r\n # Open the input_file in read mode and output_file in write mode\r\n with open(input_file, 'r') as read_obj, \\\r\n open(output_file, 'w', newline='') as write_obj:\r\n # Create a csv.reader object from the input file object\r\n csv_reader = reader(read_obj)\r\n # Create a csv.writer object from the output file object\r\n csv_writer = writer(write_obj)\r\n # Read each row of the input csv file as list\r\n for row in csv_reader:\r\n # Pass the list \/ row in the transform function to add column text for this row\r\n transform_row(row, csv_reader.line_num)\r\n # Write the updated row \/ list to the output file\r\n csv_writer.writerow(row)\r\nadd_column_in_csv('example1.csv', 'output_2.csv', lambda row, line_num: row.append(row[0] + '__' + row[1]))\r\noutput_data=pd.read_csv('output_2.csv')\r\noutput_data.head()<\/pre>\n
Output<\/p>\n
\n\n
\n
<\/th>\n
total_bill<\/th>\n
tip<\/th>\n
sex<\/th>\n
smoker<\/th>\n
day<\/th>\n
time<\/th>\n
size<\/th>\n
total_bill__tip<\/th>\n<\/tr>\n<\/thead>\n
\n
\n
0<\/th>\n
16.99<\/td>\n
1.01<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n
16.99__1.01<\/td>\n<\/tr>\n
\n
1<\/th>\n
10.34<\/td>\n
1.66<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n
10.34__1.66<\/td>\n<\/tr>\n
\n
2<\/th>\n
21.01<\/td>\n
3.50<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
3<\/td>\n
21.01__3.5<\/td>\n<\/tr>\n
\n
3<\/th>\n
23.68<\/td>\n
3.31<\/td>\n
Male<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
2<\/td>\n
23.68__3.31<\/td>\n<\/tr>\n
\n
4<\/th>\n
24.59<\/td>\n
3.61<\/td>\n
Female<\/td>\n
No<\/td>\n
Sun<\/td>\n
Dinner<\/td>\n
4<\/td>\n
24.59__3.61<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Here we see the new column is formed as the combination of the values of the 1st and 2nd column.<\/p>\n
Explanation:<\/p>\n
In the Lambda function, we received each row as a list and the line number. It then added a value in the list and the value is a merger of the first and second value of the list. It appended the column in the contents of example1.csv by merging values of the first and second columns and then saved the changes as output_2.csv files.<\/p>\n
\n
\n
Method 3-Add a list as a column to an existing csv file<\/h3>\n<\/li>\n<\/ul>\n
In this method, we will add our own value in the column by making a list of our values and pass this into the function that we will make. Let see the code for this.<\/p>\n
from csv import writer\r\nfrom csv import reader\r\ndef add_column_in_csv(input_file, output_file, transform_row):\r\n \"\"\" Append a column in existing csv using csv.reader \/ csv.writer classes\"\"\"\r\n # Open the input_file in read mode and output_file in write mode\r\n with open(input_file, 'r') as read_obj, \\\r\n open(output_file, 'w', newline='') as write_obj:\r\n # Create a csv.reader object from the input file object\r\n csv_reader = reader(read_obj)\r\n # Create a csv.writer object from the output file object\r\n csv_writer = writer(write_obj)\r\n # Read each row of the input csv file as list\r\n for row in csv_reader:\r\n # Pass the list \/ row in the transform function to add column text for this row\r\n transform_row(row, csv_reader.line_num)\r\n # Write the updated row \/ list to the output file\r\n csv_writer.writerow(row)\r\nl=[]\r\nl.append(\"New Column\")\r\nrows = len(data.axes[0])\r\nfor i in range(rows):\r\n val=i+1\r\n l.append(val)\r\nadd_column_in_csv('example1.csv', 'output_3.csv', lambda row, line_num: row.append(l[line_num - 1]))\r\noutput_data=pd.read_csv('output_3.csv')\r\noutput_data.head()<\/pre>\n
In the Lambda function, we received each row as a list and the line number. It then added a value in the list and the value is an entry from our list l at index\u00a0\u00a0line_num \u2013 1.Thus all the entries in the\u00a0list l\u00a0<\/em>are added as a column in the CSV.<\/p>\n
So these are some of the methods to add new column in csv.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"
Methods to add a column to an existing CSV File In this article, we will discuss how to add a column to an existing CSV file using csv.reader\u00a0and\u00a0csv.DictWriter\u00a0 classes. Apart from appending the columns, we will also discuss how to insert columns in between other columns of the existing CSV file. Original CSV file content …<\/p>\n