Replacing column value of a CSV file in Python Last Updated : 02 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Let us see how we can replace the column value of a CSV file in Python. CSV file is nothing but a comma-delimited file. Method 1: Using Native Python way Using replace() method, we can replace easily a text into another text. In the below code, let us have an input CSV file as "csvfile.csv" and be opened in "read" mode. The join() method takes all lines of a CSV file in an iterable and joins them into one string. Then, we can use replace() method on the entire string and can perform single/multiple replacements. In the entire string, the given text is searched and replaced with the specified text. Example: The input file will be: Python3 # reading the CSV file text = open("csvfile.csv", "r") #join() method combines all contents of # csvfile.csv and formed as a string text = ''.join([i for i in text]) # search and replace the contents text = text.replace("EmployeeName", "EmpName") text = text.replace("EmployeeNumber", "EmpNumber") text = text.replace("EmployeeDepartment", "EmpDepartment") text = text.replace("lined", "linked") # output.csv is the output file opened in write mode x = open("output.csv","w") # all the replaced text is written in the output.csv file x.writelines(text) x.close() Output: Method 2: Using Pandas DataFrame We can read the CSV file as a DataFrame and then apply the replace() method. Python3 # importing the module import pandas as pd # making data frame from the csv file dataframe = pd.read_csv("csvfile1.csv") # using the replace() method dataframe.replace(to_replace ="Fashion", value = "Fashion industry", inplace = True) dataframe.replace(to_replace ="Food", value = "Food Industry", inplace = True) dataframe.replace(to_replace ="IT", value = "IT Industry", inplace = True) # writing the dataframe to another csv file dataframe.to_csv('outputfile.csv', index = False) Output: Comment More infoAdvertise with us Next Article Replacing column value of a CSV file in Python P priyarajtt Follow Improve Article Tags : Python Python-pandas python-csv Practice Tags : python Similar Reads Reading and Writing CSV Files in Python CSV (Comma Separated Values) format is one of the most widely used formats for storing and exchanging structured data between different applications, including databases and spreadsheets. CSV files store tabular data, where each data field is separated by a delimiter, typically a comma. Python provi 4 min read Reading CSV files in Python A CSV (Comma Separated Values) file is a form of plain text document that uses a particular format to organize tabular information. CSV file format is a bounded text document that uses a comma to distinguish the values. Every row in the document is a data log. Each log is composed of one or more fie 5 min read Delete a CSV Column in Python The comma-separated values ââ(CSV) file is a delimited text file that uses commas for individual values. Each line of the file is a data record in CSV. This format used for tabular data, rows, and columns, exactly like a spreadsheet. The CSV file stores data in rows and the values ââin each row are 3 min read How to Replace Values in a List in Python? Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using 2 min read Read CSV File without Unnamed Index Column in Python Whenever the user creates the data frame in Pandas, the Unnamed index column is by default added to the data frame. The article aims to demonstrate how to read CSV File without Unnamed Index Column using index_col=0 while reading CSV. Read CSV File without Unnamed Index Column Using index_col=0 whil 2 min read Like