Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of reading and writing CSV files by Pandas

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "Pandas on the method of reading and writing CSV documents", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "Pandas's method of reading and writing CSV documents".

What is a CSV file

A CSV file (comma-separated value file) is a plain text file that uses a specific structure to arrange table data. Because it is a plain text file, it can only contain actual text data, in other words, printable ASCII or Unicode characters.

Typically, the structure of an CSV file is given by its name, separating each specific data value with a comma.

Column 1 name,column 2 name,column 3 namefirst row data 1,first row data 2,first row data 3second row data 1,second row data 2,second row data 3...

How each piece of data is separated by commas. The name of the first behavior data column, sometimes it can be empty. The first row is the actual data. Each subsequent line is actual data, limited only by file size.

Where do CSV files come from?

CSV files are usually created by programs that process large amounts of data. Export data from spreadsheets and databases and import it in other programs. For example, you can export the results of a data mining program as a CSV file, and then import it into a spreadsheet to analyze the data, generate charts for presentation, or prepare for publishing reports.

CSV files are very easy to deal with programmatically. Any language that supports text file input and string manipulation, such as Python, can work with CSV files directly.

CSV Library parses CSV Files

The csv library provides the ability to read and write CSV files. Designed to use CSV files generated by Excel out of the box, adapting to a variety of CSV formats. The csv library contains objects and other code for reading, writing, and processing data from CSV files.

Read CSV file

The CSV file is opened as a text file using Python's built-in open () function, which returns a file object. Then use the reader object to finish reading from the CSV file.

Employee_birthday.txt

Name,department,birthday monthJohn Smith,Accounting,NovemberErica Meyers,IT,March

The method of reading directly.

Import csvwith open ('employee_birthday.txt') as csv_file: csv_reader = csv.reader (csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count = 0: print (f'Column names are {" ".join (row)}') line_count + = 1 else: print (f'\ t {row [0]} works in the {row [1]} department, and was born in {row [2]}.') Line_count + = 1 print (f'Processed {line_count} lines.')

The method of reading in a dictionary way.

Import csvwith open ('employee_birthday.txt', mode='r') as csv_file: csv_reader = csv.DictReader (csv_file) line_count = 0 for row in csv_reader: if line_count = = 0: print (f'Column names are {"," .join (row)}') line_count + = 1 print (f'\ t {row ["name"]} works in the {row ["department"]} department And was born in {row ["birthday month"]}.') Line_count + = 1 print (f'Processed {line_count} lines.')

The final output is the same.

Column names are name, department, birthday month

John Smith works in the Accounting department, and was born in November.

Erica Meyers works in the IT department, and was born in March.

Processed 3 lines.

CSV reader parameter

The reader object can handle different styles of CSV files by specifying additional parameters.

Delimiter specifies the characters used to separate each field, and the default value is a comma (',').

Quotechar specifies the character used to enclose the field that contains the delimiter, and the default value is double quotation marks ('").

Escapechar specifies the character used to escape the delimiter in case quotation marks are not used, and there are no escape characters by default.

Employee_addresses.txt

Name,address,date joinedjohn smith,1132 Anywhere Lane Hoboken NJ, 07030,Jan 4erica meyers,1234 Smith Lane Hoboken NJ, 07030,March 2

This CSV file contains three fields, name, address, and date joined, separated by commas. The problem is that the data in the address field also contains a comma to represent the zip code.

Think about how to deal with this?

Writing of CSV file

Writing to the CSV file can be done using the. Write _ row () method.

Import csvwith open ('employee_file.csv', mode='w') as employee_file: employee_writer = csv.writer (employee_file, delimiter=',', quotechar=' ", quoting=csv.QUOTE_MINIMAL) employee_writer.writerow ([' John Smith', 'Accounting',' November']) employee_writer.writerow (['Erica Meyers',' IT', 'March'])

Quotechar is used to surround fields with special characters to eliminate ambiguity.

Several kinds of quoting control quotation mark behavior:

Csv.QUOTE_NONNUMERIC) # non-numeric quotation marks

Csv.QUOTE_ALL # all fields in quotation marks

Csv.QUOTE_MINIMAL # Special fields in quotation marks

Csv.QUOTE_NONE # without quotation marks

Written in a dictionary.

Import csvwith open ('employee_file2.csv', mode='w') as csv_file: fieldnames= [' emp_name', 'dept',' birth_month'] writer = csv.DictWriter (csv_file, fieldnames=fieldnames) writer.writeheader () writer.writerow ({'emp_name':' John Smith', 'dept':' Accounting', 'birth_month':' November'}) writer.writerow ({'emp_name':' Erica Meyers') 'dept':' IT', 'birth_month':' March'})

Employee_file2.csv

Emp_name,dept,birth_monthJohn Smith,Accounting,NovemberErica Meyers,IT,March uses the pandas library to parse CSV files

Pandas is an open source Python library that provides high-performance data analysis tools and easy-to-use data structures to share data, code, analysis results, visualization, and narrative text.

Pandas reads CSV files

Hrdata.csv

Name,Hire Date,Salary,Sick Days remainingGraham Chapman,03/15/14,50000.00,10John Cleese,06/01/15,65000.00,8Eric Idle,05/12/14,45000.00,10Terry Jones,11/01/13,70000.00,3Terry Gilliam,08/12/14,48000.00,7Michael Palin,05/23/13,66000.00,8

You can read it quickly using pandas.

Import pandasdf = pandas.read_csv ('hrdata.csv') print (df) Name Hire Date Salary Sick Days remaining0 Graham Chapman 03 Terry Jones 15 + 14 50000.0 101 John Cleese 06 * 01 * Terry Gilliam 08/12/14 48000.0 75 Michael Palin 05/23/13 66000.0 8

Date format can be formatted when reading data using pandas.

Import pandasdf = pandas.read_csv ('hrdata.csv', index_col='Name' Parse_dates= ['Hire Date']) print (df) Hire Date Salary Sick Days remainingName Graham Chapman 2014-03-15 50000.0 10John Cleese 2015-06-01 65000.0 8Eric Idle 2014-05-12 45000.0 10Terry Jones 2013-11 -01 70000.0 3Terry Gilliam 2014-08-12 48000.0 7Michael Palin 2013-05-23 66000.0 8pandas write to CSV file

What is read to pandas can be written directly to the new csv file.

Import pandasdf = pandas.read_csv ('hrdata.csv', index_col='Employee', parse_dates= [' Hired'], header=0, names= ['Employee',' Hired', 'Salary',' Sick Days']) df.to_csv ('hrdata_modified.csv') print (df) Employee,Hired,Salary,Sick DaysGraham Chapman,2014-03-15 10John Cleese,2015-06-01 8Eric Idle 2014-05-12 45000 Jones,2013 10 Terry Gilliam,2014-11-01 Magneology 70000 Gilliam,2014-08-12 3Terry Gilliam,2014-08-12 Magical48000.0Power7Michael Palin,2013-05-23 66000.0recover8 is the content of this article on "Pandas's methods for reading and writing CSV files" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report