SlideShare a Scribd company logo
1 of 18
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 (20)

Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Relational model
Relational modelRelational model
Relational model
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
11 Database Concepts
11 Database Concepts11 Database Concepts
11 Database Concepts
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Array in Java
Array in JavaArray in Java
Array in Java
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashing
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
Data Analysis and Visualization using Python
Data Analysis and Visualization using PythonData Analysis and Visualization using Python
Data Analysis and Visualization using Python
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
File organization and introduction of DBMS
File organization and introduction of DBMSFile organization and introduction of DBMS
File organization and introduction of DBMS
 

Similar to Python and CSV Connectivity

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.pptxKirti Verma
 
Introduction to sas
Introduction to sasIntroduction to sas
Introduction to sasAjay Ohri
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to HiveDatabricks
 
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)Mark Kromer
 
Reading_csv.pptx
Reading_csv.pptxReading_csv.pptx
Reading_csv.pptxOpOp39
 
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 filesKiran Kumaraswamy
 
Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8Introduction to-sas-1211594349119006-8
Introduction to-sas-1211594349119006-8thotakoti
 
Python Pandas.pptx
Python Pandas.pptxPython Pandas.pptx
Python Pandas.pptxSujayaBiju
 
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 HiveWill Du
 
Pandas-(Ziad).pptx
Pandas-(Ziad).pptxPandas-(Ziad).pptx
Pandas-(Ziad).pptxSivam Chinna
 
BAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureBAS 150 Lesson 4 Lecture
BAS 150 Lesson 4 LectureWake Tech BAS
 
Mapping Data Flows Training April 2021
Mapping Data Flows Training April 2021Mapping Data Flows Training April 2021
Mapping Data Flows Training April 2021Mark Kromer
 
Machine learning session 3
Machine learning session 3Machine learning session 3
Machine learning session 3NirsandhG
 
Tabular Data on the Web
Tabular Data on the WebTabular Data on the Web
Tabular Data on the WebGregg Kellogg
 

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

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.pptxNeeru Mittal
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in PythonNeeru Mittal
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python PandasNeeru Mittal
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and TricksNeeru Mittal
 
Working of while loop
Working of while loopWorking of while loop
Working of while loopNeeru Mittal
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
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 workingNeeru Mittal
 
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++Neeru Mittal
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programmingNeeru Mittal
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 
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++ 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

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

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