Pandas read_csv() method is used to read CSV file into DataFrame object. The CSV file is like a two-dimensional table where the values are separated using a delimiter.

熊猫的read_csv()方法用于将CSV文件读取到DataFrame对象中。 CSV文件就像一个二维表,其中的值使用定界符分隔。

1.熊猫read_csv()示例 (1. Pandas read_csv() Example)

Let’s say we have a CSV file “employees.csv” with the following content.

假设我们有一个包含以下内容的CSV文件“ employees.csv”。

Emp ID,Emp Name,Emp Role
1,Pankaj Kumar,Admin
2,David Lee,Editor
3,Lisa Ray,Author

Let’s see how to read it into a DataFrame using Pandas read_csv() function.

让我们看看如何使用Pandas read_csv()函数将其读取到DataFrame中。

import pandasemp_df = pandas.read_csv('employees.csv')print(emp_df)

Output:

输出:

Emp ID      Emp Name Emp Role
0       1  Pankaj Kumar    Admin
1       2     David Lee   Editor
2       3      Lisa Ray   Author
Recommended Reading: 推荐读物 : Python Pandas TutorialPython Pandas教程

2.使用Pandas read_csv()函数指定分隔符 (2. Specifying Delimiter with Pandas read_csv() function)

The default delimiter of a CSV file is a comma. But, we can use any other delimiter too. Let’s say our CSV file delimiter is #.

CSV文件的默认定界符是逗号。 但是,我们也可以使用任何其他定界符。 假设我们的CSV文件分隔符为#。

Emp ID#Emp Name#Emp Role
1#Pankaj Kumar#Admin
2#David Lee#Editor
3#Lisa Ray#Author

In this case, we can specify the sep parameter while calling read_csv() function.

在这种情况下,我们可以在调用read_csv()函数时指定sep参数。

import pandasemp_df = pandas.read_csv('employees.csv', sep='#')print(emp_df)

Output:

输出:

Emp ID      Emp Name Emp Role
0       1  Pankaj Kumar    Admin
1       2     David Lee   Editor
2       3      Lisa Ray   Author

3.仅从CSV文件中读取特定的列 (3. Reading only specific Columns from the CSV File)

We can specify usecols parameter to read specific columns from the CSV file. This is very helpful when the CSV file has many columns but we are interested in only a few of them.

我们可以指定usecols参数来从CSV文件读取特定的列。 当CSV文件包含许多列但我们仅对其中几列感兴趣时,这将非常有用。

import pandasemp_df = pandas.read_csv('employees.csv', usecols=['Emp Name', 'Emp Role'])print(emp_df)

Output:

输出:

Emp Name Emp Role
0  Pankaj Kumar    Admin
1     David Lee   Editor
2      Lisa Ray   Author

4.读取没有标题的CSV文件 (4. Reading CSV File without Header)

It’s not mandatory to have a header row in the CSV file. If the CSV file doesn’t have header row, we can still read it by passing header=None to the read_csv() function.

在CSV文件中包含标题行不是强制性的。 如果CSV文件没有标题行,我们仍然可以通过将header=None传递给read_csv()函数来读取它。

Let’s say our employees.csv file has the following content.

假设我们的employee.csv文件具有以下内容。

1,Pankaj Kumar,Admin
2,David Lee,Editor

Let’s see how to read this CSV file into a DataFrame object.

让我们看看如何将此CSV文件读取到DataFrame对象中。

import pandasemp_df = pandas.read_csv('employees.csv', header=None)print(emp_df)

Output:

输出:

0             1       2
0  1  Pankaj Kumar   Admin
1  2     David Lee  Editor
2  3      Lisa Ray  Author

Notice that the column headers are auto-assigned from 0 to N. We can pass these column values in the usecols parameter to read specific columns.

请注意,列标题是从0到N自动分配的。我们可以在usecols参数中传递这些列值以读取特定的列。

import pandasemp_df = pandas.read_csv('employees.csv', header=None, usecols=[1])print(emp_df)

Output:

输出:

1
0  Pankaj Kumar
1     David Lee

5.在CSV文件中指定标题行 (5. Specifying Header Row in the CSV File)

We can also specify the row for the header value. Any rows before the header row will be discarded. Let’s say the CSV file has the following data.

我们还可以为标题值指定行。 标头行之前的任何行都将被丢弃。 假设CSV文件包含以下数据。

# some random data
invalid data
Emp ID,Emp Name,Emp Role
1,Pankaj Kumar,Admin
2,David Lee,Editor
3,Lisa Ray,Author

The header data is present in the 3rd row. So we have to pass header=2 to read the CSV data from the file.

标题数据位于第三行。 因此,我们必须传递header=2才能从文件中读取CSV数据。

import pandasemp_df = pandas.read_csv('employees.csv', header=2)print(emp_df)

Output:

输出:

Emp ID      Emp Name Emp Role
0       1  Pankaj Kumar    Admin
1       2     David Lee   Editor
2       3      Lisa Ray   Author

6.跳过CSV行 (6. Skipping CSV Rows)

We can pass the skiprows parameter to skip rows from the CSV file. Let’s say we want to skip the 3rd and 4th line from our original CSV file.

我们可以传递skiprows参数来跳过CSV文件中的行。 假设我们要从原始CSV文件中跳过第三行和第四行。

import pandasemp_df = pandas.read_csv('employees.csv', skiprows=[2, 3])print(emp_df)

Output:

输出:

Emp ID      Emp Name Emp Role
0       1  Pankaj Kumar    Admin

7.为Pandas read_csv()函数指定解析器引擎 (7. Specifying Parser Engine for Pandas read_csv() function)

Let’s say our CSV file delimiter is ‘##’ i.e. multiple characters.

假设我们的CSV文件分隔符为“ ##”,即多个字符。

Emp ID##Emp Name##Emp Role
1##Pankaj Kumar##Admin
2##David Lee##Editor
3##Lisa Ray##Author

Let’s see what happens when we try to read this CSV file.

让我们看看尝试读取此CSV文件时会发生什么。

import pandasemp_df = pandas.read_csv('employees.csv', sep='##')print(emp_df)

Output:

输出:

/Users/pankaj/Documents/PycharmProjects/AskPython/hello-world/journaldev/pandas/pandas_read_csv.py:5: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.emp_df = pandas.read_csv('employees.csv', sep='##')Emp ID      Emp Name Emp Role
0       1  Pankaj Kumar    Admin
1       2     David Lee   Editor
2       3      Lisa Ray   Author

We can avoid the warning by specifying the ‘engine’ parameter in the read_csv() function.

我们可以通过在read_csv()函数中指定'engine'参数来避免警告。

emp_df = pandas.read_csv('employees.csv', sep='##', engine='python')

There are two parser engines – c and python. The C parser engine is faster and default but the python parser engine is more feature complete.

有两个解析器引擎– c和python。 C解析器引擎更快且默认,但python解析器引擎功能更完善。

8.参考 (8. References)

  • pandas read_csv() API Doc熊猫read_csv()API文档

翻译自: https://www.journaldev.com/33316/pandas-read-csv-reading-csv-file-to-dataframe

熊猫read_csv()–将CSV文件读取到DataFrame相关推荐

  1. csv文件读取与写出

    文章目录 一.pandas读取csv文件 二.pandas写出csv文件 三.利用csv模块读取csv文件 四.利用csv模块写出csv文件 一.pandas读取csv文件 1.导入pandas包 i ...

  2. Java CSV文件读取、写入及追加

    Java CSV文件读取.写入及追加 https://blog.csdn.net/liq816/article/details/81286472 追加: FileOutputStream out = ...

  3. Python中利用numpy将数组(矩阵)存成csv文件,将csv文件读取为数组(矩阵)

    Python中利用numpy将数组(矩阵)存成csv文件,将csv文件读取为数组(矩阵) 本博客转载自:https://blog.csdn.net/vernice/article/details/50 ...

  4. python读取第二行_从CSV文件读取第二行到Python

    我有一个csv文件:Index,X1,X2,X3,X4,X5,Y 1,-1.608052,-0.377992,1.204209,1.313808,1.218265,1 2,0.393766,0.630 ...

  5. 怎么用python打开csv文件_使用Python从CSV文件读取数据

    CSV文件,也就是Comma-separated Value文件,用sublime打开是这样(数据下载见文末): 如果用excel打开是这样(千万别点保存,保存就有问题): 来看看怎么打开,如果安装了 ...

  6. 天池csv转成grt代码里的luna的csv,pandas库来操作csv文件(pd.DataFrame,pd.concat,pd.Series,to_csv等)实现

    ps之前已经稍微处理过相关的csv文件,但是没有记录,发现基本忘光了看来记录还是一件非常重要的事情.碰巧DSB2017grt团队的代码里用的csv比较奇葩,我就把天池的数据的csv改成他们使用的模样. ...

  7. pandas 读csv文件 TypeError: Empty 'DataFrame': no numeric data to plot

    简单的代码,利用pandas模块读csv数据文件,这里有两种方式,一种是被新版本pandas遗弃的Series.from_csv:另一种就是pandas.read_csv 先说一下问题这个问题就是在读 ...

  8. python中如何打开csv文件_在Python中从CSV文件读取数据

    我正在从包含以下数据的CSV文件(xyz.CSV)中读取数据: col1,col2,col3,col4 name1,empId1,241682-27638-USD-CIGGNT ,1 name2,em ...

  9. CSV文件读取和处理

    CSV的特点 每行文本都呈现为行,字段被分隔符分隔开.这个分隔符通常是逗号,有时也是制表符. 不需要使用专门的CSV软件来导入CSV文件.使用最简单的文本编辑器就可以打开CSV文件. 对于大多数编程语 ...

最新文章

  1. Grunt手表错误 - 等待...致命错误:观看ENOSPC
  2. VB6.0 怎样启用控件comdlg32.ocx
  3. Java StringBuffer 方法
  4. mysql内连接和交叉连接_MySQL中的内连接、外连接、交叉连接
  5. c++如何让类对象只能在堆(栈)上分配空间
  6. [UI列表]LoopScrollRect无限滑动不卡顿
  7. Android控件 - TextView、Button、EditText、CompoundButton、CheckBox简介
  8. python做bi系统_如何用开源bi,打造自己的轻量级bi系统
  9. Java九大内置对象
  10. Enriching Local and Global Contexts for Temporal Action Localization
  11. Ubuntu系统安装ghostscript seq2logo
  12. 避免内容失去焦点_痛楚难以避免,而磨难可以选择
  13. LeetCode 37. 解数独 Sudoku Solver
  14. 如何快速查询京东快递物流正在派送中的单号
  15. 计算机毕业设计Java城市出行行程智能推荐系统(源码+系统+mysql数据库+lw文档)
  16. OSI模型 四层发现-nmap(诸神之眼)工具介绍
  17. PS如何批量处理图片大小
  18. Oracle 10g的闪回机制
  19. L2-4 彩虹瓶 (25分) 栈的运用
  20. selenium打不开Ie浏览器的解决办法

热门文章

  1. 【Assembly】Mixed mode dll unable to load in .net 4.0
  2. 很多人搞不清楚的两个类Vector,ArrayList
  3. 汇编:输出寄存器AX中的内容
  4. C语言指针的高级操作
  5. POJ3264(分桶法)
  6. 20150917-html第二次课
  7. SSI(Server Side Includeds)使用详解(转载)
  8. cvCreateStructuringElementEx理解
  9. error: #error This file requires compiler and library support for the ISO C++ 2011 standard.
  10. GPIO_Pin和GPIO_PinSource的大不同