Subject: Informatics Practices (JHADESWAR
Chapter-4 CSV File Handling
CSV File
‘© CSV (Comma Separated Values) is a simple file format used to store tabular data,
such as a Spreadsheet or Database.
* ACSV file stores tabular data (number and text) in plain'text:
* Each line of the file is a data record.
+ Each record consists of one or more fields) separated by commas.
© There is an in-built module called esv, for working With CSV files.
Advantages of CSV Format: AN
* Asimple, compact and ubiquitous format for data storage.
* Acommon format for data interchange.
‘* It can be opened in popular spreadsheet packages like MS Excel, OpenOffice Calc
etc.
* Nearly all spreadsheets and database support import/export to csv format.
Created By- SK ABDULISRAR
PGT (Computer Science) MTech, MCA, BCA CContact-7008443534, 9090042626
Subject : Informatie Practices &, JHADESWAR
CChapter-4 CSV File Handling
Creating CSV File
A CSV isa text file, so it can be created and edited by any text editor (Notepad) and
Spreadsheet applications (MS Excel, OpenOffice Calc).
All CSV files follow a standard format, i.e., each column is separated by a delimiter
{such as comma, semicolon, space or tab) and each line indicates a new row.
a) Creating CSV File using Notepad
© Start >> Notepad >> Ctrl +N
© Write the contents using delimiter (comma)
* Save the File (Ctrl+S) >> Give a filename (e.g. emp1.csv)
>> Choose Save as type >> All Files >> Save.
‘Created By-SK ABDUL ISRAR
PGT (Computer Science) MTech, MCA, BCA Sepa TOORAS PA, SOMERSJHADESWAR
Chapter CSV File Handling Uternational school
a
ID,NAME,CITY,SALARY,BONUS
101,KISORE,CHENNAL,35000,4000
102,ANITA, MUMBAT,45000,6500
103,SWARUP, KOLKATA, 18000,1800
104, JUGAL, PUNE,25000,3000
105,PUNAM,DELHI,33000,2500
b) Creating CSV File using Excel
* Start >> Excel >> Blank Workbook
* Write the contents in‘row and column
(Created By-SK ABDULISRAR
PGT (Computer Science) MTech, MCA, BCA ‘Contact-7008443534, 9090042625
Subject : Informatics Practices JHADESWAR
‘Chapter-4 CSV File Hanaing eraational
# Save the File (Ctrl+S) >> Give a filename (e.g. emp2) >> Choose Save as type >>
CSV (Comma delimited) >> Save.
Reading from a CSV File to Dataframe
‘* The read_csv() function in pandas, loads the data in a Pandas Dataframe.
syntax:
pd.read_csv(“filename.csv")
* CSV file treats all datatypes as characters.
© Pandas interprets datatypes specifically when loading the data.
Ex:
CCreated By-SK ABDUL ISRAR
PGT (Computer Science) MTech, MCA, BCA Contact-7008443534, 9090042626Subject: Informats Practices
(Chapter-4 CSV File Handling
import pandas as pd
df=pd.read_csv("emp1.csv")
print(dt)
ofp:
Finding number of rows and columns from a CSV
‘We can see the total number of rows and'collimns by using shi
Bx ,
import pandas as pd 7
df=pd.read_csv("empt.csv")
print("Shape is ",df-shape)
row,col=df.shape,
print('Number of rows :,row)
print(‘Number of Columns :,co!)
‘Created By- SK ABDULISRAR
PGT (Computer Science) MTech, MCA, BCA
Subject : Informatics Practices
CChapter-4 CSV File Handling,
Writing a CSV file with default index
JHADESWAR
iaternational Sehoot
Empl Name Age City Salary
25,0 Mumbai 25000.0
280 delhi 780000
MAN NaN NaN NaN
10,0 Sanjay 23.0 Kolkata 290000
103.0 Aman 26.0 Chennai 260000
5.1040 Anita 27.0 Mumbai 38000.0
16 105.0 Kajol 23.0 Delhi 450000
7 106.0 Suman 280 Kolkats 250000
8 107.0 Swagat 290 Chennai 310000
‘command.
Seana eee!
Shape is: (9,5)
Number of rows: 9
Number of Columns :5
bAternational S
‘= To create a CSV file from a dataframe, the to_csv() method is used.
© We can do this either by transferring the records directly to the CSV file or by copying the contents of
the original CSV file to another file.
© The attribute index=False is used to skip in the index as a column in the new CSV file.
Ex: (To Copy one CSV file another new CSV file)
import pandas as pd
fepd.read_csv("emp1.csv")
df-to_csv("empnew.csv'