📜  如何在使用 Pandas 读取 csv 文件时跳过行?

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

如何在使用 Pandas 读取 csv 文件时跳过行?

由于以数据为中心的Python包的生态系统令人惊叹, Python是一种很好的数据分析语言。 Pandas 包就是其中之一,它使导入和分析数据变得更加容易。

在这里,我们将讨论如何在读取 csv 文件时跳过行。我们将使用 Pandas 库的read_csv()方法来完成此任务。

下面给出了一些有用的参数:

ParameterUse
filepath_or_bufferURL or Dir location of file
sepStands for separator, default is ‘, ‘ as in csv(comma separated values)
index_colThis parameter is use to make passed column as index instead of 0, 1, 2, 3…r
headerThis parameter is use to make passed row/s[int/int list] as header
use_colsThis parameter is Only uses the passed col[string list] to make data frame
squeezeIf True and only one column is passed then returns pandas series
skiprowsThis parameter is use to skip passed rows in new data frame
skipfooterThis parameter is use to skip Number of lines at bottom of file

如需下载 student.csv 文件,请单击此处

方法 1:读取 csv 文件时从头开始跳过 N 行。

代码:

Python3
# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from start in csv
# and initialize it to a  dataframe
df = pd.read_csv("students.csv",
                  skiprows = 2)
 
# Show the dataframe
df


Python3
# Importing Pandas library
import pandas as pd
 
# Skipping rows at specific position
df = pd.read_csv("students.csv",
                  skiprows = [0, 2, 5])
 
# Show the dataframe
df


Python3
# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from start
# except the column names
df = pd.read_csv("students.csv",
                 skiprows = [i for i in range(1, 3) ])
 
# Show the dataframe
df


Python3
# Importing Pandas library
import pandas as pd
 
# function for checking and
# skipping every 3rd line
def logic(index):
 
    if index % 3 == 0:
        return True
 
    return False
 
# Skipping rows based on a condition
df = pd.read_csv("students.csv",
                 skiprows = lambda x: logic(x) )
 
# Show the dataframe
df


Python3
# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from end
df = pd.read_csv("students.csv",
                  skipfooter = 5,
                  engine = 'python')
 
# Show the dataframe
df


输出 :

csv文件内容

方法 2:在读取 csv 文件时跳过特定位置的行。

代码:

Python3

# Importing Pandas library
import pandas as pd
 
# Skipping rows at specific position
df = pd.read_csv("students.csv",
                  skiprows = [0, 2, 5])
 
# Show the dataframe
df

输出 :

csv 文件内容_6

方法 3:读取 csv 文件时,除了列名之外,从开头跳过 N 行。

代码:

Python3

# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from start
# except the column names
df = pd.read_csv("students.csv",
                 skiprows = [i for i in range(1, 3) ])
 
# Show the dataframe
df

输出 :

csv 文件内容_5

方法 4:在读取 csv 文件时根据条件跳过行。

代码:

Python3

# Importing Pandas library
import pandas as pd
 
# function for checking and
# skipping every 3rd line
def logic(index):
 
    if index % 3 == 0:
        return True
 
    return False
 
# Skipping rows based on a condition
df = pd.read_csv("students.csv",
                 skiprows = lambda x: logic(x) )
 
# Show the dataframe
df

输出 :

csv 文件内容_4

方法 5:读取 csv 文件时从末尾跳过 N 行。

代码:

Python3

# Importing Pandas library
import pandas as pd
 
# Skipping 2 rows from end
df = pd.read_csv("students.csv",
                  skipfooter = 5,
                  engine = 'python')
 
# Show the dataframe
df

输出 :

csv 文件内容_3