SlideShare a Scribd company logo
Guided Discovery of
interfacing CSV files with
Python
Pandas DataFrame and CSV files: Interfacing
This Photo by Unknown Author is licensed under CC BY-SA
• Pandas DataFrame is a two dimensional
structures with row and columns.
• Due to its inherent tabular structure, we
can query and run calculations on pandas
dataframes across an entire row, an entire
column, or a specific cell or series of cells
based on either location and attribute
values.
• CSV files are a very common file format
used to collect and organize scientific
data.
• CSV files use commas (or some other
delimiter like tab spaces or semi-colons)
to indicate separate values.
• CSV files also support labeled names for
the columns, referred to as headers. This
means that CSV files can easily support
multiple columns of related data.
This Photo by Unknown Author
is licensed under CC BY-SA
CSV files: Comma Separated Files
We will learn how to import tabular data from text files (.csv) into pandas
dataframes, so we can take advantage of the benefits of working with pandas
dataframes.
Statistical
functions
Data
Analysis
Why interface CSV with Python Pandas?
Importing CSV files using Pandas DataFrame
import pandas as pd
result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv")
print(result)
Command to read a CSV file
Storage path of the file
Data Frame
Output
read_csv : Syntax
DataFrame = pd.read_csv (“filepath”, sep=“ “, header=0)
• name of the comma separated data file along with its path.
• sep specifies whether the values are separated by comma, semicolon, tab, or any
other character. The default value for sep is a space.
• header specifies the number of the row whose values are to be used as the
column names. It also marks the start of the data to be fetched. By default,
header=0.
1 2 3
1
2
3
Variation in sep parameter
import pandas as pd
result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv“, sep=“,”,
header=0)
print(result)
Output
Variation in the header parameter
import pandas as pd
result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv“, sep=“,”,
header=1)
print(result)
Output
Using the Name parameter
We can specify our own column names using the parameter names while creating
the DataFrame using the read_csv() function.
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv", header=0,
names=["Roll_no","Studname", "Math","English","Science", "Arts"])
print(result)
Note: We need to specify the
Header=0 parameter here otherwise,
there will be a double heading.
Output
Changing Index column
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv",
index_col='Enroll_no')
print(result)
Output
Using DataFrame functions
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv")
print(result.head(2))
print(result.tail(2))
Output
Applying Pandas Aggregate Functions
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv")
print(result.sum())
print(result.sum(axis=1))
print(result['Marks1'].sum())
print(result[‘Marks1’])
print(result.mean())
print(result.loc[2])
print(result.loc[1:4])
Applying Pandas Aggregate Functions
Adding a new column
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv")
result["Marks6"]=[22,11,13,15]
print(result)
Output
Adding a new row
import pandas as pd
result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv")
result.loc[4]=[22,24,25,20.20]
print(result)
Output
Exporting DataFrames to CSV
import pandas as pd
marksUT= {'Name':['Raman','Zuhaire','Mishti','Drovya'],
'UT':[1,2,3,4],
'Maths':[22,21,14,20],
'Science':[25,22,21,18],
'S.St':[18,17,15,22],
'Hindi':[20,22,15,17],
'Eng':[24,23,13,20]
}
pd1=pd.DataFrame(marksUT)
pd1.to_csv(path_or_buf='C:/Users/Neeru/Desktop/Docs/dd.csv’,
sep=',')
DataFrame
Creation
File PathCommand to write to a CSV file
The to_csv command
DataFrame.to_csv(path_or_buf=“File path”, sep=',')
• name of the comma separated data file along with its path.
• sep specifies that the values are separated by a comma.2
21
1
Attendance link
docs.google.com/forms/d/e/1FAIpQLSdTbUb1khFFMxh6di8G8mpbrfrh98ppn0poOdQDypmchq741Q/
viewform?vc=0&c=0&w=1

More Related Content

What's hot

What's hot (20)

Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Pandas
PandasPandas
Pandas
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 

Similar to Python and CSV Connectivity

Reading_csv.pptx
Reading_csv.pptxReading_csv.pptx
Reading_csv.pptx
OpOp39
 
Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8
thotakoti
 

Similar to Python and CSV Connectivity (20)

SAS - Training
SAS - Training SAS - Training
SAS - Training
 
Pandas Dataframe reading data Kirti final.pptx
Pandas Dataframe reading data  Kirti final.pptxPandas Dataframe reading data  Kirti final.pptx
Pandas Dataframe reading data Kirti final.pptx
 
Pa2 session 2
Pa2 session 2Pa2 session 2
Pa2 session 2
 
Introduction to sas
Introduction to sasIntroduction to sas
Introduction to sas
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to Hive
 
Azure Data Factory Data Flows Training (Sept 2020 Update)
Azure Data Factory Data Flows Training (Sept 2020 Update)Azure Data Factory Data Flows Training (Sept 2020 Update)
Azure Data Factory Data Flows Training (Sept 2020 Update)
 
Reading_csv.pptx
Reading_csv.pptxReading_csv.pptx
Reading_csv.pptx
 
CSV Files-1.pdf
CSV Files-1.pdfCSV Files-1.pdf
CSV Files-1.pdf
 
Pa1 session 5
Pa1 session 5Pa1 session 5
Pa1 session 5
 
Dealing with files in python specially CSV files
Dealing with files in python specially CSV filesDealing with files in python specially CSV files
Dealing with files in python specially CSV files
 
Moving Data to and From R
Moving Data to and From RMoving Data to and From R
Moving Data to and From R
 
Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8
 
Python Pandas.pptx
Python Pandas.pptxPython Pandas.pptx
Python Pandas.pptx
 
Ten tools for ten big data areas 04_Apache Hive
Ten tools for ten big data areas 04_Apache HiveTen tools for ten big data areas 04_Apache Hive
Ten tools for ten big data areas 04_Apache Hive
 
Pandas-(Ziad).pptx
Pandas-(Ziad).pptxPandas-(Ziad).pptx
Pandas-(Ziad).pptx
 
CSV_FILES.pptx
CSV_FILES.pptxCSV_FILES.pptx
CSV_FILES.pptx
 
BAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureBAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 Lecture
 
Mapping Data Flows Training April 2021
Mapping Data Flows Training April 2021Mapping Data Flows Training April 2021
Mapping Data Flows Training April 2021
 
Machine learning session 3
Machine learning session 3Machine learning session 3
Machine learning session 3
 
Tabular Data on the Web
Tabular Data on the WebTabular Data on the Web
Tabular Data on the Web
 

More from Neeru Mittal

More from Neeru Mittal (18)

Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptx
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Nested loops
Nested loopsNested loops
Nested loops
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 

Python and CSV Connectivity

  • 1. Guided Discovery of interfacing CSV files with Python
  • 2. Pandas DataFrame and CSV files: Interfacing This Photo by Unknown Author is licensed under CC BY-SA • Pandas DataFrame is a two dimensional structures with row and columns. • Due to its inherent tabular structure, we can query and run calculations on pandas dataframes across an entire row, an entire column, or a specific cell or series of cells based on either location and attribute values. • CSV files are a very common file format used to collect and organize scientific data. • CSV files use commas (or some other delimiter like tab spaces or semi-colons) to indicate separate values. • CSV files also support labeled names for the columns, referred to as headers. This means that CSV files can easily support multiple columns of related data. This Photo by Unknown Author is licensed under CC BY-SA
  • 3. CSV files: Comma Separated Files We will learn how to import tabular data from text files (.csv) into pandas dataframes, so we can take advantage of the benefits of working with pandas dataframes.
  • 5. Importing CSV files using Pandas DataFrame import pandas as pd result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv") print(result) Command to read a CSV file Storage path of the file Data Frame Output
  • 6. read_csv : Syntax DataFrame = pd.read_csv (“filepath”, sep=“ “, header=0) • name of the comma separated data file along with its path. • sep specifies whether the values are separated by comma, semicolon, tab, or any other character. The default value for sep is a space. • header specifies the number of the row whose values are to be used as the column names. It also marks the start of the data to be fetched. By default, header=0. 1 2 3 1 2 3
  • 7. Variation in sep parameter import pandas as pd result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv“, sep=“,”, header=0) print(result) Output
  • 8. Variation in the header parameter import pandas as pd result = pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv“, sep=“,”, header=1) print(result) Output
  • 9. Using the Name parameter We can specify our own column names using the parameter names while creating the DataFrame using the read_csv() function. import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv", header=0, names=["Roll_no","Studname", "Math","English","Science", "Arts"]) print(result) Note: We need to specify the Header=0 parameter here otherwise, there will be a double heading. Output
  • 10. Changing Index column import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv", index_col='Enroll_no') print(result) Output
  • 11. Using DataFrame functions import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv") print(result.head(2)) print(result.tail(2)) Output
  • 12. Applying Pandas Aggregate Functions import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv") print(result.sum()) print(result.sum(axis=1)) print(result['Marks1'].sum()) print(result[‘Marks1’])
  • 14. Adding a new column import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv") result["Marks6"]=[22,11,13,15] print(result) Output
  • 15. Adding a new row import pandas as pd result=pd.read_csv ("C:/Users/Neeru/Desktop/Docs/b.csv") result.loc[4]=[22,24,25,20.20] print(result) Output
  • 16. Exporting DataFrames to CSV import pandas as pd marksUT= {'Name':['Raman','Zuhaire','Mishti','Drovya'], 'UT':[1,2,3,4], 'Maths':[22,21,14,20], 'Science':[25,22,21,18], 'S.St':[18,17,15,22], 'Hindi':[20,22,15,17], 'Eng':[24,23,13,20] } pd1=pd.DataFrame(marksUT) pd1.to_csv(path_or_buf='C:/Users/Neeru/Desktop/Docs/dd.csv’, sep=',') DataFrame Creation File PathCommand to write to a CSV file
  • 17. The to_csv command DataFrame.to_csv(path_or_buf=“File path”, sep=',') • name of the comma separated data file along with its path. • sep specifies that the values are separated by a comma.2 21 1