0% found this document useful (0 votes)
16 views10 pages

L CsvReadWrite

Uploaded by

priyanshu9107
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)
16 views10 pages

L CsvReadWrite

Uploaded by

priyanshu9107
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/ 10

1.

21 CSV FILE
CSV (Comma Separated Values) is a simple file format used to store tabular
spreadsheet or database. ACSV file stores tabular data (numbers and text) in
data, such
plain text.
line of the file is adata record. Each record consists of one or more fields, separated
by Each
The use of the comma as afield separator is the source of the name for this file format commas
For working CSVfiles in Python,there is an in-built module called csv. Files of this format :are
used to exchange data, usually when there is alarge amount, between different applicationsgeneral y
1.22 DATA TRANSFER BETWEEN DATAFRAMES AND .CSV FILE
CSV format is a kind of tabular data separated by comma and is stored in the form of plain +.
Roll No Name Marks
Roll No, Name, Marks
101 Ramesh 77.5 After conversion to CSV Format 101,Ramesh, 77.5
102 Harish 45.6 102, Harish, 45.6
Tabular Data CSV File
Fig. 1.4: Tabular Data vs CSV Data
In CSV format:
" Each row of the table is stored in one row in a CSV file.
" The field values of a row are stored together with comma after
every field value.
Advantages of CSV format:
" A simple, compact and universal format for data
storage.
" Acommon format for data interchange.
" It can be opened in popular spreadsheet packages like MS
Excel, OpenOffice Calc, etc.
Nearly allspreadsheets and databases support
import/export to CSV format.
CTM: CSV is a simple file format used to store
tabular data, such as a spreadsheet or database.
1.22.1 Creating and Reading CSV File
ACSV is a text file so it can be
created and edited using any text editor. More
aCSV file is created by exporting a frequently, however,
spreadsheet or database in the program that
All CSV files followa standard created it.
format, i.e., each column is separated by a
Comma, semicolon, space or a tab) and each new line delimiter (such as a
indicates a new roW.
Let us create a CSV file using
Microsoft Excel on the basis of "Employee" table.
Table 1.1: Employee
Empid Name Age City Salary
100
Ritesh 25 Mumbai 15000 7.
101 Aakash 26 Goa 16000
102
Mahima 27 Hyderabad
103 20000
104
Lakshay 23 Delhi
18000
Manu 25 Mumbai
105 Nidhi 26 Delhi
25000
106 Geetu 30 Bengaluru
28000
1.58 Informatics Practices with Python-X|
1. Launch Microsoft Excel.
2. Type the data given in Table 1.1 in the Excel sheet (Fig. 1.5). You willalso notice that some
cell values are missing to represent missing values (NaN) in Pandas dataframe.
Booki Microsoft Excel
Home Insert Page Layout Formulas Dsts Reiew Addtns

Cut Calibri WIsp fet Genersl

Paste
Copy
J Format Pairter L Merge &Center
Cipboard Font Alignment Humber

P13

A D G H

1 Empid Namne Age City Salary


100 Ritesh 25 Mumbai 15000
101 Aakash 26 Goa 16000

5 102 Mahima 27 Hyderabad 20000


6 103 Lakshay 23 Delhi 18000
7 104 Manu 25 Mumbai 25000
8 105 Nidhi 26 Delhi
106 Geetu 30 Bengaluru 28000
10

11

Fig. 1.5: Microsoft Excel Worksheet for Employee


Save As
3. Save the file with a proper
‘ This PC New Volume (E) Data Search Dete
name by clicking File -> Save or
Organize v New folder
Save As or press Ctrl + Sto open Date modified Type
o Downloads Name
the Save As window as shown Music
No items match your search.
Pictures
in Fig. 1.6. Videos
Windows8 0s
4. Type the name of the file as G lENOVO (D:)
Employee and select file type G New Volume (E

as CSV (Comma delimited) ¬ Network


(".csv) from the drop-down
arrow (Fig. 1.6). File name|Employee.csv
Save as type CSV (Comma delimited) (.csv)
5. Click the Save button. Excel will Authors: preeti Tags: Add a tag

ask for confirmation to select


Tools Save Cancel
Hide Folders
CSV format.
6. Click OK as shown in ig. 1.7. Fig. 1.6: Saving the Worksheet in CSV Format

X
Microsoft Office Excel

The selected fle type does not support workbooks that contain multicle sheets.
"To save only the acove sheet, dd OK.
that supports mulbiple sheets.
"To save al sheets, save hem individualy using a different fle name for each, or hoosea fle type
OK Cancel

Fig. 1.7: Seeking permission to save in CSV format


1.8).
7. Itwill display a dialog box asking permission to keep comma as delimiter for CSV file (Fig.
Microsoft Office Excel

Employee.cy may contain features that are not compatble vith CS (Comma delimited). Do you want to keep the workbook in this format?

1 T o keeo this format, vwhih leaves out any incompatblein features, dd Yes.
"To preserve the feaures, dik No. Then save a copy he latest Excel format.
"To see what might be lost, didk Help.
Yes NG Help

Fig. 1.8: Setting delimiter for CSV format


1 50
8. Lastly, click Yes to retainand save the Employee.csv - Notepad
Excel file in CSV format. File Edit Format View Help
Empid, Name, Age, City,Salary
To view this CSV file, open any Text 100, Ritesh, 25, Mumbai, 15000
101,Aakash, 26,Goa, 16000
Editor (Notepad preferably) and
explore the folder containing 102,Mahima, 27, Hyderabad,20000
103,Lakshay, 23, Delhi, 18000
Employee.csv file. (In this case, 104, Manu, 25,Mumbai, 25000
105, Nidhi,26, Delhi,
the path for CSV file is: "E:\Data\ 106,Geetu,30, Bengaluru,28000
Employee.csv").
Fig. 1.9: CSV file contentsusing Notepad
If you open the file in a Notepad editor, you wíll observe that each
column is separated b.
comma 0) delimiter and each new line indicates a new row/record (Fig. 1.9).
1.22.2 Reading from a CSV File to Dataframe
After creating a simple "Employee" CSV file, it can be read using
once youknow the path of your file. The read_csv(0 function loads the read_csv() function in Pandas
data in aPandas dataframe
We know that multiple dataframes have
multiple data types, i.e., some
numbers,some are float, strings or dates, etc. But CSV file treats allthese columns are integers or
data types as characters
only. However, Pandas interprets these data types
if a column contains only numbers, Pandas will specifically when loading the data. For example,
set that column's datatype to an integer or float.
Syntax for read_csv() method is:
import pandas as pd
<df>=pd. read_ csv (<FilePath> wseco led, c,c3 . . | n o : n,
Practical Implementation-49 Skihrow[= n desl Nene
To create and open
"Employee.csv" filenames Lnenn,nenm.] inseeto)
using Pandas.
prog_csvdf1.py -
File Edit Format Run
#To open C/Users/preeti/AppData/Local/Programs/Python/Python37-32/prog_csv_df1.py
Employee.csv
Options Window Help
into a Data Frame
(3.7.0)

import pandas as pd
-pd.read csv
print (df) ("E:\\Data\\Employee.csv") #Select the proper path of your file

The first line imports the Ln: 9 Cok 0

Pandas module. The read_CSV


dataframe 'df". pd.read method loads the data in a Pandas
the command prompt as_csv("path") shall fetch the data from csv file and
shown in the output window. disnlav all records at
File Edit Shell Debug Python 3.7.0 Shell
LAldLIOII. Options Window Help
>>>

RESTART: C:/Users
1
2
Python37-32/prog csv_dfl.py
Empid
100.0
101.0
NaN
Ritesh
Aakash
Name
/preeti/AppData/Local/Programs/Python
Age
25.0
26.0
city
Mumbai
Goa
Salary
15000.0
16000.0
/

3 NaN NaN
102.0 NaN NaN
4 l03.0 Mahimna 27.0 Hyderabad 20000,0
5 104.0 Lakshay
Manu
23.0 Delhi 18000.0
6
105.0 25.0 Mumbai
7 Nidhi 26.0 25000.0
106.0 Geetu Delhi NaN
>>> 30.0 Bengaluru
28000.0
Ln: 14 Col: 4
1.60, Informatics Practices with Python-XII
One thing tobe rememberedis that the missing values from the CSV file shall be treated as NaN
(Not a Number) in Pandas dataframe.
Practical Implementation-50
To display the shape (number of rows and columns) of the CSV file.
We can see the total number of rows (records) and columns (fields) present in the table with
the help of shape command.
Empid Name Age City Salary
100.0 Ritesh 25.0 Mumbai 15000.0
1 101.0 Aakash 26.0 Goa 16000.0
2 NaN NaN NaN NaN NaN
3 102.0 Mahima 27.0 Hyderabad 20000.0
4 103.0 Lakshay 23.0 Delhi 18000.0
5 104.0 Manu 25.0 Mumbai 25000.0
6 105.0 Nidhi 26.0 Delhi NaN
7 106.0 Geetu 30.0 Bengaluru 28000.0
>>> df. shape
(8, 5)

>>> roW,column =df.shape


|>>> rOW
8
>>> column
5

In the above case, we have directly displayed the row count and column count at Python shell
prompt by giving the command as df.shape. We can also display it using variables.
the
The read_csv) method automatically takes the first row of the csv file and assigns it as
dataframe header. After the creation of dataframe from aCSV file, you can perform all the
dataframe operations on it.
Reading CSV file with specific/selected columns
several columns contained in
While working with large tables in CSV format, there can be
into a dataframe. This can be done by
it. But you may require selective columns to be read
For example, in the case of
using "usecols" attribute or option along with read_csv) method.
This can be done by
"Employee" table, you have to access Name, Age and Salary of employees.
giving the command as:
("E:\\Data\\Employee.csv",
>>> df = pd.read csv
usecols = ['Name', 'Age','Salary' ])
>>> df

Practical Implementation-51
Employee.csv.
To display Name, Age and Salary from
usecols =('Name','Age', 'Salary)
>>> df = pd.read csv ("E:\\Data\\Employee.csv",
>>> df
Name Age Salary
Ritesh 25.0 15000.0
1 Aakash 26.0 16000.0
2 NaN NaN NaN
3 Mahima 27.0 20000.0
Lakshay 23.0 18000.0
Manu 25.0 25000.0
6 Nidhi 26.0 NaN
Geetu 30.0 28000.0
7
>>>
Data Handling using Pandas 1.61
Reading CSV file with specific/selected rows
Like columns,there can be thousands of records in acsv file. You can display selective
rows or selective lines using "nrows" option or attribute used with read_csv(0 method. This e records|
be done by giving the command as:
>>> df = pd.read csv("E:\\Data\\Employee. csv",nrows=5)
>>> print (df)
Practical Implementation-52
To display only 5 records from Employee.csv.
&prog.cv_df1.py - C\Users\preetiNAppData\ Local Programs\Python\ Pyth.- - Dx
File Edit Format Run Options Window Help
#To open Employee.csv for selective rows only

import pandas as pd
|df -pd.read csv("E:\\Data\\Employee.csv",nrows = 5)
print (df)

L8 Cok 0

Inthe above code, we have given 5 as the value to 'nrows' attribute used with read_csv) function.
nrows means number of rows. In the above example, 5 represents the first five records, even empty
records containing NaN values, excluding headers. Hence, the following output shall be obtained.
>>>
RESTART: C:\Users\preeti\AppData\Local\Programs \
Python\Python37-32\prog_cSv_dfl.py
Empid
100.0
Name City Age Salary
Rítesh 25.0 Mumbai 15000.0
1 101.0 Aakash 26.0
NaN NaN NaN
Goa 16000.0
NaN NaN
3 102.0 Mahima 27.0
4
Hyderabad 20000.0
103.0 Lakshay 23.0 Delhi 18000.0
>>>

Reading CSV file without header


If you do not want to display the first
row as the header for dataframe using
then this can be done by assigning None Employee table,
value for 'header' argument or 'skiprows'
using read_csv() method. argument
Practical Implementation-53
To display records without header.
G prog_csv_dfl.py -
Fle Edt Fomat Run
#To open
CAUsers\preeti\AppData\Local\Programs\Python\Python37
Options Window Help
O
Employee.csv without header
import pandas as pd
df -pd.read csv
prínt (d£) ("E:\\Data\\Employee. csv", header -None)
In the above example, None is the Lx8 Cet 0

dataframe value passed to


will contain the
header information as the'header' argument; as a
first row, shoWn in theresult, the resultant
RESTART: C:\Users\preetí\AppData\Local\ Programs\
Python\Python37-32\prog_csv_dfl.py output below:
1 2
Eupid 3
100 Name Age
Ritesh 25 City salary
l01 Aakash Mumbai 15000
3 26
NaN Goa 16000
102 NaN NaN NaN
103 Mahima 27 Hyderabad NaN
6
104
105
Lakshay
Manu
23
25
Delhi
20000
18000
106
Nidhí
2¬ Mumbai 25000
>>> Geetu Delhi
30 Bengaluru
28000
NaN
Reading CSV file without index
You can also read and load the records into the dataframe without displaying their respective
index numbers, which are displayed by default, by specifying the attribute index_col =0 using
the read_csv() method.
Practical Implementation-54
To display records without index numbers.
t prog csv, dfl.py -cUsers\preet\AppData\LocaNPrograms\Python\Python37.
Fle EdR Format Run Options Window Help
#To open Enployee. csv without index numbers
import pandas as pd
áf pd.read csv("E:\\Data\ \Employee. csv", index_col =0)
print (df)
Python 3.7.0 Shell
File Edit Shell Debug Options Window Help
RESTART : C:\Users\preeti\AppData\Local\ Programs\
Python\Python37-32\prog_csv dfl.py
Name Age City Salary
Empid
100.0 Ritesh 25.0 Mumbai 15000.0
101.0 Aakash 26.0 Goa 16000.0
NaN NaN NaN NaN NaN
102.0 Mahima 27.0 Hyderabad 20000.0
103.0 Lakshay 23.0 Delhi 19000.0
104.0 Manu 25.0 Mumbai 25000.0
105.0 Nidhí 26.0 Delhi NaN
106.0 Geetu 30.0 Bengaluru 28000.0
Ln: 57 Cok 4

displayed along with records.


As shown in the above output window, there are no index numbers
indexes.
Now Empid will be treated as the first column instead of
Reading CSV file with new column names
with new column names, i.e., you can
You can read and load data from CSV file into adataframe
exists, you have to skip it using
rename the columns while reading the .csv file. If the header
renaming the columns.
skiprowS argument along with names argument for
Practical Implementation-55
names.
To display Employee file with new column \Local\Programs\Python\Python37-32\prog.csv df1.- D
prog_csy_df1.py -CAUsersipreeti\AppData
Fe Edt Fomat Run Options
Window Help
With new Column names
#To open Employee. CsV
#Renaming Columns
import pandas as pd
("E:\\Data\\Employee.csv",skiprows =1,
df =pd. read csv names =('E id', 'Ename ', 'E_ age','Ecity', 'Esalary'])
print (df)

column
code, we have given the option skiprows = 1which will omit the default
In the above displayed while
argument holds the new column names to be
names from the CSV file. names
following output shall be displayed:
loading the CSV file into the dataframe. Hence, the
>>> C:\Users\preeti\AppData\Local\Programs\Python\ Python3
RESTART:
7-32\prog_csv_dfl .py Ecity Esalary
E id Ename E age
Mumbai 15000.0
0 100.0 Ritesh 25.0
Goa 16000.0
Aakash 26.0
1 101.0 NaN NaN
NaN NaN
2 NaN 20000.0
102.0 Mahima 27.0 Hyderabad
3 Delhi 18000.0
103.0 Lakshay 23.0
4 MImbai 25000.0
Manu 25.0
5 104.0 Delhi NaN
Nidhi 26.0
6 105.0 30.0 Bengaluru 28000.0
|7 106.0 Geetu
>>>
1.23 UPDATING/MODIFYING CONTENTS IN ACSV FILE
In the previous section, we learnt how to change column name. Similarly, we can modify
update row data as well.
We will now discuss ways in which the value(s) of a column can be updated. The best and th.
optimalway to update any column value of aCSV is to use the Pandas Library and the dataframe
functions. The following steps are to be followed for updating column contents in a CSV file usine
dataframe.
Import module
Open CSV file and read itsdata
Find column to be updated
Update value in the CSV file using to_csv)function
Consider the Employee.csv used in the previous implementations. Suppose we wish to change
the employee name (Ename) from Mahima to Harsh.
Practical Implementation-56
To modify the employee name from Mahima to Harsh.
>>>

RESTART: C:/Users/preeti/AppData/Local/Programs.
updtecol.py
Dataframe Contents before updation
Empid Name Age City Salary
100.0 Ritesh 25.0 Mumbai 15000.0
101.0 Aakash 26.0 Goa 16000.0
2 NaN NaN NaN NaN NaN
3 102.0 Mahima 27.0 Hyderabad 20000.0
4 103.0 Lakshay 23.0 Delhi 18000.0
5 104.0 Manu 25.0 Mumbai 25000.0
6 105.0 Nidhi 26.0 NaN NaN
106.0 Geetu 30.0 NaN 28000.0

L prog_dfccsV_ updtecol.py- c/Users/preeti/AppData/Local/Prog.


File Edit Format Run Options Window Help
#To change the column contents of the dataframe
#generated using .csv file-Employee.csv
import pandas as pd
df =pd.read csv ("E:\\Data\\Employee.csv")
print ("Dataframe Contents before updation")
print (af)
print ()
# updating the column value/data
df.l0c(3, 'Name']="Harsh"
# writing into the file
df.to csv
("E:\\Data\\Employee.csv",
print ("Dataframe index=False)
Contents after updation")
print (df)
Ln: 17 Col: 0

Dataframe Contents after updation


Empid Name Age
0 100,0 Ritesh
City Salary
25.0 Mumbai 15000.0
101.0 Aakash 26.0 Goa
2 NaN NaN NaN
16000.0
NaN NaN
3 102.0 Harsh 27.0 Hyderabad 20000.0
|4 103.0 Lakshay 23.0 Delhi 18000.0
5 104.0 Manu 25.0 Mumbai 25000.0
6 105.0 Nidhi 26.0 NaN NaN
106.0 Geetu 30.0 NaN 28000.0
As observed from the screenshots obtained for the output and the contents before updating the CSV
file, it is clear that the name of the employee has been changed/modified from Mahima to Harsh.
to_csv) method converts the dataframe into CSV data as the output is returned to the file, it
takes the file object or the file name as the parameter and the index=False should be mentioned
sothat the indices are not written into the CSV file.
Hence, the modification has been done for employee name as shown in the output window.

1.24 WRITING ACSV FILE WITH DEFAULT INDEX


this either by
To create a CSV file from a dataframe, the to csv() method is used. We can do
transferring the records directly to the CSV file or by copying the contents of the original
CSV file to another file.

Copying Employee.csv to Empnew.csv


We can copy/write the data, i.e., create a duplicate copy of Employee.csv as Empnew.csv.
Practical Implementation-57
To create a new CSV file by copying the contents of Employee.csv.
prog_emp_csvnevw.py- C/Users/preeti/AppData/Local/Programs/.
File Edit Format Run Options Window Help
#Copying the contents of Employee . csv to Empnew

import pandas as pd
df -pd. read csv ("E:\\Data\\Employee. csv")
df.to csv ("E:\\Data\\Empnew.csv")
print (df)
Ln: 10 Cot 0

file shall be created containing


Upon executing the above commands, Empnew.csv (duplicate)
values. You can browse the folder to see
the same contents as Employee.csV with default index
below:
the contents of the newly-created file Empnew.csv as shown
Data

File Home Share

Nev item Open Seled all

DCopy path
X
Delete Renace New
DEasy acces Properties History
9 seled none
Invert seledion
CoPy
Copy Paste Paste shortcut folder
New Open Select
Clipboard Organize
Search Data
This PC New Volume (E) Data
(’ ‘

DName
Empnew.V- Microsoft Excel
KFavortes Formulas Data Review View Add-Ins
Employeecsv Home Insert Page Layout
Desktop
General
lo Downtoads ZEnpnew.cN Calibi 11

Recent places
B |A EI9 %
Styles Cells
Paste
S| A 2
Font Alignment Number Editing
OneDrive Clipboard

This Pc G H
A C

Desktop Empid Name Age City Salary


D Documents 100 Ritesh 25 Mumbal 15000
Dounoads 1 101 Aakash 26 Goa 16000

Ausic
102 Mahima 27 Hyderaba 20000
Pictues
4 103 Lakshay 23 Delhl 18000
a Videos 6
104 Manu 25 Mumbal 25000
Windouse_OS (C)
G LNONO (D) Nidhi 26 Delhi
23000
GNex Volume (E) 106 Geetu 30 Bengaluru
10

GNetaotk

file Empnew are the same as


As shown in the above screenshot, the contents of the duplicate
user to change the data or add new
that of Employee.csv. Creating a duplicate file allows the data. In such acase,
the original
data to the already existing Employee.csv file without affecting
with the previous data using the
Pandas provides us with the optionof creating a duplicate file
above-mentioned command.
1.25 SAVING DATAFRAME AS CSV FILE
This is the direct approach of creating a CSV file by first creating a dataframe and
it into a CSV file format. then loading
Practical Implementation-58
To create a student CSV file from dataframe.
>>> Student ={'RollNo': [1,2, 3,4,5, 6),
'studName':[Teena', 'Rinku',' Payal', 'Akshay', 'Garvit','Yogesh'1,
'Marks': [90, 78, 88, 89, 77,97),
'Class ':'11A',"11B', '11c', '11A', '11D',11E']}
>>> import pandas as pd
>>> df =pd.DataFrame (Student, columns =('RollNo', 'studName', 'Marks',
'Class'])
>>> df.to csv ("E:\\Data\\Student.csv"),

This will create Student.csv file in the Data folder. If you


open this file in a spreadsheet like
MS Excel, youwill get the Student data in the form of rows/records and
columns as shown below:
Data
File Home Share View

X Cut
Copy Paste
Copy path Student.cSV - Microsoft Excel
D Paste shortcut Move Copy Delete Rename
to to Home Insert Page Layout Formulas Data
Clipboard
Review View Add-Ins o x
Organize Calibri |1 General
- ‘ A
This PC New Volume (E) Data BiI U-AIE %
Paste
A Styles Cells
Favorites DName Clipboard G Font
2
Alignment G Number
Editing
Desktop
lo Downloads Employee.csv
Empnew.csv A
AJ

Recent places C D G
Student.csv 1 RollNo StudName Marks Class
H

SOneDrive 1 Teena 90 11A


3 1 2 RinkU 78 118
4 2
This Pc 3 Payal 88 11C
3 4 Akshay
k Desktop 89 11A
6
S Garvit
Documents 7 S
6 Yogesh
77 11D
97 11E
Downloads 3
WMusic
Pictures 10
a Videos 11
Windows8_0S (C) 12
G LENOVO (D:)
G New Volume (E)
15
Network 16
4 Student
Our next step is to read the
Student.csv into a dataframe using read csv()
File Edit Shell Python 3.7.0 Shel method.
Debug Options Window Help
>>> df.to csv
>>> df pd.read csv ("E:\\Data\\Student.csv")
>>> df ("E:\\Data\\Student.csv")
Unnamed: 0
RollNo StudName Marks Class
1
1 1 Teena 90 11A
2
2 2 Rinku 78 11B
3
3 3 Payal 88 11c
4
5 5
4
5 Akshay
Garvit
89 11A
6 77 11D
>>> Yogesh 97 11E

Ln: 43 Cot 4
In the given output window, Unnamed: 0column gets displayed automatically along with the index
values. To avoid this column,use the attribute index_col =0 with read csv() method as shown:
Python 3.7.0 Shell
File Edit Shell Debug Options Window Help

>>> df -pd.read csv("E:\\Data\\Student. .csv", index_col -0)


>>> df

RollNo StudName Marks Class


1 Teena 90 11A
2 Rinku 11B
3 Payal 88 11c
3 4 Akshay 89 11A
Garvit 77 11D
5 6 Yogesh 97 11E
>>>
Ln: 54 Col: 4

1.26 COPYING FIELDS INTO A NEW FILE


complete set of columns and
With reference to section 1.24, we have created Employee.csv file with
file containing only the selected fields.
rows. In certain situations,it isrequired to create a duplicate
Practical Implementation-59
and Employee name.
Tocreate a duplicate file for Employee.csv containing Empid
C/Users/preeti/AppData/Local/Programs/Python/Python37-32/pro.
G prog csV_Create.py -
File Edit Format Run Options Window Help
columns
#Creating a duplicate csv file with selective
import pandas as pd
df =pd.read csv ("E:\\Data\
\Employee.csv")
'Name'])
dfl = df.to csv("E:\ \Data\\Emp.csv",columns =["Empid",
Ln: 10 Col: 0

Data

File Home Share View


New item Open Seled all
Cut

Copy path
Xs
Delete Rename Neww
Easy access
Properties
2Edit 8 Select none
Invert seledion
Move Copy History
Copy Paste Paste shortcut to to folder
Salad

Clipboard
Organize Emp.csy - Microsoft Excel
New Volume (E) Data Review View Add-Ins O X
‘ This PC Home Insert Page Layout Formulas Data

Name Calibri 11
Favorites BI U AA Alignment Number Styles Cells
Desktop Emp.ca Paste
A 2
De Downloads Employee.csv Font
Editing
Empnew.csv Clipboard
Recent places A1
Student.csv F G
A C D E
OneDrive Name
Empid
100 Ritesh
2
This PC 3 101 Aakash
Desktop 4
B Documents 102 Mahima
o Downloads 103 Lakshay
WMusic 7 104 Manu
Pictures 3 6 105 Nidhi
a Videos 106 Geetu
B, Windows8_ OS (C) 10
G LENOVO (D:) 11
G New Vlume (E) 12
K-4b Emp
O 100%
Network Ready

(case-sensitive).
are the same in both the files
Learning Tip: Ensure that field names

You might also like