Updating modifying contents in a CSV Files : In this section we will show how to read and update CSV file in python. We will show how to change particular value in csv file.
What is CSV File ?
A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it’s a plain text file, it can contain only actual text data. CSV files use a comma to separate each specific data value. Here’s what that structure looks like. In general, the separator character is called a delimiter, and the comma is not the only one used. Other popular delimiters include the tab (\t
), colon (:
) and semi-colon (;
) characters. Properly parsing a CSV file requires us to know which delimiter is being used.
In this section we will learn how to change the CSV File data. we can modity or update row data as wel.. Suppose we want to replace the salary of the employee whse salary is 15000 with NaN values. This can be done by using na_values option along with read_csv method for te respective salary as 15000.
import pandas as pd
df=pd.read_csv("C:\\Users\\IP1\\Desktop\\Employee.csv",na_values=[15000])
print(df)
# Copy the content of one file to another file
import pandas as pd
df=pd.read_csv("C:\\Users\\IP1\\Desktop\\Employee.csv",na_values=[15000])
print(df)
print("copy the content of the file to another csv file")
#copy the content of the file to another csv file
df.to_csv("C:\\Users\\IP1\\Desktop\\Employee11.csv")
print(df)
In this section of This article we will Save Dataframe Value as CSV File. to do this we need to create a dataframe of student class and use to_csv() function in this program.
import pandas as pd
student={"RollNo":[1,2,3,4,5,6],
"StudName":["Teena","Meena","Kuku","Priya","Kunal","Garvit"],
"Marks":[90,78,88,89,77,97],
"Class":["11","12","11","12","10","11"]}
df=pd.DataFrame(student,columns=["RollNo","StudName","Marks","Class"])
df.to_csv("C:\\Users\\IP1\\Desktop\\Student.csv")
print(df)
In this section of this article we will learn about how to save dataframe as csv File and how to copy specific columns to new csv Files. we will use to_copy function to do this . Follow below program to save and copy columns in new CSV file.
import pandas as pd
student={"RollNo":[1,2,3,4,5,6],
"StudName":["Teena","Meena","Kuku","Priya","Kunal","Garvit"],
"Marks":[90,78,88,89,77,97],
"Class":["11","12","11","12","10","11"]}
df=pd.DataFrame(student,columns=["RollNo","StudName","Marks","Class"])
df.to_csv("C:\\Users\\IP1\\Desktop\\Student.csv")
print(df)
df=pd.read_csv("C:\\Users\\IP1\\Desktop\\Employee.csv")
df1=df.to_csv("C:\\Users\\IP1\\Desktop\\Employee12.csv",columns=["E_id","Ename","Ecity"])
print(pd.read_csv("C:\\Users\\IP1\\Desktop\\Employee12.csv"))
0 Comments