0% found this document useful (0 votes)
71 views14 pages

XII IP CH 4 Importing Exporting

Jfzjgsjgd

Uploaded by

tanishq7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views14 pages

XII IP CH 4 Importing Exporting

Jfzjgsjgd

Uploaded by

tanishq7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Importing/ Exporting Data

between CSV files and Pandas


CSV Files
• The acronym CSV is short for Comma
Separated Values.
• It refers to a tabular data that has been saved
as plain text where data is separated by
commas.
• Each row of the table is stored in one row.
• The field values of a row are stored together
with commas after every field value, except
the last value which has end of line after it.
Advantages of CSV format
• A simple, compact and universal format for
data storage.
• A common format for data interchange.
• It can be opened in popular spreadsheet
packages like MS Excel, Calc, etc.
• Nearly all spreadsheets and databases support
import/ export to csv format.
Connecting CSV to DataFrames
• Python's Pandas library offers two functions to
read/ write data from csv files:
– read_csv()
Help you bring data from a CSV file into a
dataframe.

– to_csv()
Write a dataframe's data to a CSV file.
Reading from a CSV file to DataFrame
• Consider a file Example1.csv stored in G: drive

Excel View Notepad View

The commands used to read the file:


<DF>=pandas.read_csv(<filepath and name>)
import pandas as pd
df=pd.read_csv("G:\\Example1.csv")
print(df)

NOTE: The first row from the CSV file is taken as


the column names for the DataFrame df.
If the CSV file does not contain headings, then

import pandas as pd
df=pd.read_csv("G:\\Example2.csv", header=None)
print(df)
Specifying column headings
If the CSV file does not contain headings, then

import pandas as pd
df=pd.read_csv("G:\\Example2.csv",
names=["Rollno", "FirstName", "LastName"])
print(df)
Specifying column headings
If the CSV file contains headings, then

import pandas as pd
df=pd.read_csv("G:\\Example1.csv",
names=["Country", "Code", "Year",
"Population"], skiprows=1)
print(df)
sep argument
• Some csv files are so created that their
separator character is different from comma
such as ; | tab(\t), etc.

df=pd.read_csv("G:\\Example3.csv", sep=';')

df=pd.read_csv("G:\\Example3.csv", sep='\t')
Get DataFrame Index labels from CSV
import pandas as pd
df=pd.read_csv("G:\\Example3.csv",
index_col="Empno")
print(df)
Storing DataFrame's Data to CSV File
• Function to_csv() can be used to save the data
of a DataFrame to a CSV file.

<DF>.to_csv(<filepath>)
<DF>.to_csv(<filepath>, sep=<separator char>)
d={'Rollno':[101,102,103,104],
'Name':['Ani','Amna','Arjan','Mark'],
'Surname':['Kapur','Sharif','Singh','Robin']}
df=pd.DataFrame(d)
print(df)
df.to_csv("G:\\student.csv")

You might also like