📜  Python|使用 pandas.read_csv() 读取 csv

📅  最后修改于: 2022-05-13 01:55:12.402000             🧑  作者: Mango

Python|使用 pandas.read_csv() 读取 csv

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas 就是其中之一,它使导入和分析数据变得更加容易。
导入熊猫:

import pandas as pd


代码 #1:read_csv是一个重要的 pandas函数,用于读取 csv 文件并对其进行操作。

PYTHON3
# Import pandas
import pandas as pd
 
# reading csv file
pd.read_csv("filename.csv")


PYTHON3
# importing Pandas library
import pandas as pd
 
pd.read_csv(filepath_or_buffer = "pokemon.csv")
 
# makes the passed rows header
pd.read_csv("pokemon.csv", header =[1, 2])
 
# make the passed column as index instead of 0, 1, 2, 3....
pd.read_csv("pokemon.csv", index_col ='Type')
 
# uses passed cols only for data frame
pd.read_csv("pokemon.csv", usecols =["Type"])
 
# returns pandas series if there is only one column
pd.read_csv("pokemon.csv", usecols =["Type"],
                              squeeze = True)
                               
# skips the passed rows in new series
pd.read_csv("pokemon.csv",
            skiprows = [1, 2, 3, 4])


通过它打开 CSV 文件很容易。但是通过这个函数可以做很多其他的事情,只是完全改变返回的对象。例如,不仅可以在本地读取 csv 文件,还可以通过 read_csv 从 URL 读取,或者可以选择需要导出的列,这样我们以后就不必编辑数组了。
这是它所采用的参数列表及其默认值

并非所有这些都很重要,但记住这些实际上可以节省自己执行相同功能的时间。通过在 jupyter notebook 中按 shift + tab 可以查看任何函数的参数。下面给出了有用的和它们的用法:

ParameterUse
filepath_or_bufferURL or Dir location of file
sepStands for separator, default is ‘, ‘ as in csv(comma separated values)
index_col

Makes passed column as index instead of 0, 1, 2, 3…r 
 

 

header

Makes passed row/s[int/int list] as header
 

 

use_colsOnly uses the passed col[string list] to make data frame
squeezeIf true and only one column is passed, returns pandas series
skiprowsSkips passed rows in new data frame

请参阅此处使用的数据集的链接。
代码#2:

Python3

# importing Pandas library
import pandas as pd
 
pd.read_csv(filepath_or_buffer = "pokemon.csv")
 
# makes the passed rows header
pd.read_csv("pokemon.csv", header =[1, 2])
 
# make the passed column as index instead of 0, 1, 2, 3....
pd.read_csv("pokemon.csv", index_col ='Type')
 
# uses passed cols only for data frame
pd.read_csv("pokemon.csv", usecols =["Type"])
 
# returns pandas series if there is only one column
pd.read_csv("pokemon.csv", usecols =["Type"],
                              squeeze = True)
                               
# skips the passed rows in new series
pd.read_csv("pokemon.csv",
            skiprows = [1, 2, 3, 4])