Python技术迷

Pandas 教程-如何在 Pandas 中删除行

整理:python架构师

使用 pandas.DataFrame.drop() 我们可以删除或消除给定 DataFrame 中的行。我们可以使用 DataFrame.axis 参数选择要删除的轴。默认情况下,axis=0 意味着删除行。要删除列,应用 axis=1 或 columns 参数。在删除行时,默认情况下,Pandas 会创建 DataFrame 的副本;要从引用的现有 DataFrame 中删除,请使用 inplace=True 选项。

语法:
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')  

参数:

  • labels: 由字符串或字符串集合引用的行或列标签。

  • axis: 整数或字符串项,分别具有行索引为 0 和列索引为 1。

  • index 或 columns: 单个标签或列表。索引或列可以替换标签,但不能同时使用。

  • level: 当数据框具有多个级别的索引时,用于定义级别。

  • inplace: 如果为 True,则更新原始数据框。

  • errors: 如果列表中的任何项为 False,则忽略错误,并在 errors 设置为 "ignore" 时删除其余值。

返回类型: 更新后的 DataFrame

使用行索引标签删除单个行

Pandas 的一个优势是提供类似于列名的行标签或标题。如果数据框支持行标签(也称为索引标签),我们可以使用行标签名称指定要删除的单个行。

代码
# Python program to delete a single row using row labels  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  # returning a new DataFrame after dropping the row having index label 'B'  new_df = df.drop('B')  print("Original DataFrame: \n", df)  print("New DataFrame: \n", new_df)  

专属福利 

👉点击领取:最全Python资料合集
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17New DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32C  Harry   28            38D  Naill   25            17

使用索引标签删除多个行

我们可以在列表中给 drop 命令提供多个行索引标签,以从 DataFrame 中删除多个行。

代码
# Python program to delete multiple rows using row labels  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  # returning a new DataFrame after dropping the row having index labels 'B' and 'C'  new_df = df.drop(['B', 'C'])  print("Original DataFrame: \n", df)  print("New DataFrame: \n", new_df)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17New DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32D  Naill   25            17

通过索引号删除行

同样,我们可以通过将索引位置作为 drop() 方法的参数提供来从给定的 Pandas DataFrame 中删除行。由于 drop() 方法不接受行的位置索引作为参数,因此我们必须使用索引并将其传递给 drop() 方法。为了获取要删除的 DataFrame 的行名称,我们必须使用 df.index 函数提取行名称。对于我们打算删除的索引,我们将使用 df.index.values 函数以列表形式返回所有行名称。

例如,使用 df.index[[1,2]],获取第二行和第三行的行标签;然后,drop() 方法删除这些行。请记住,Python 中的列表索引从零开始。

代码
# Python program to delete rows using index number of rows  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  # returning a new DataFrame after dropping the row having index labels 'B' and 'C' using their index positions  new_df = df.drop([df.index[1], df.index[2]])  print("Original DataFrame: \n", df)  print("New DataFrame: \n", new_df)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17New DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32D  Naill   25            17

原地删除 DataFrame 的行

到目前为止,我们一直在获取删除行的新 DataFrame。然而,我们可以在不创建新数据框的情况下删除数据框的行,称为对数据框执行 'in place' 操作。

代码
# Python program to delete rows of a DataFrame in place  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  print("Original DataFrame: \n", df)  # Updating the existing DataFrame after dropping the row having index labels 'B' and 'C' using their index positions  df.drop([df.index[1], df.index[2]], inplace = True)  print("Updated DataFrame: \n", df)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17Updated DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32D  Naill   25            17

使用索引范围删除行

通过定义索引范围,我们也可以删除行。下面的示例删除了第三行之前的所有行。

代码
# Python program to delete a range of rows  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  print("Original DataFrame: \n", df)  # Updating the DataFrame after dropping the row before index 3  df.drop(df.index[:3], inplace = True)  print("Updated DataFrame: \n", df)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17Updated DataFrame:      Name  Age  Salary (LPA)D  Naill   25            17

根据条件检查删除行

通过 loc[] 和 iloc[] 函数,我们可以根据某些条件(列值)轻松删除 DataFrame 行。

代码
# Python program to delete rows based on a particular condition  # importing the required library  import pandas as pd  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, 20, 38, 17]  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  print("Original DataFrame: \n", df)  # Deleting rows having people of age lesser than 25  df1 = df.loc[df['Age'] < 25]  print("New DataFrame: \n", df1)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32B  Peter   26            20C  Harry   28            38D  Naill   25            17New DataFrame:      Name  Age  Salary (LPA)A  Itika   21            32

删除包含 NaN/None 值的行

在处理分析时,我们经常需要清理包含 None、Null 和 np.NaN 值的行。我们可以通过调用 df.dropna() 删除给定 DataFrame 中的 NaN 值。

代码
# Python program to delete rows having NAN values  # importing the required library  import pandas as pd  import numpy as np  # Creating a dictionary to store data  data = {      'Name' : ['Itika', 'Peter', 'Harry', 'Naill'],      'Age' : [21, 26, 28, 25],      'Salary (LPA)' : [32, np.NaN, 38, np.NaN],  }  # creating a DataFrame for the above data  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Salary (LPA)'],                    index = ['A', 'B', 'C', 'D'])  # Dropping rows having NAN values  new_df = df.dropna()  print("Original DataFrame: \n", df)  print("New DataFrame: \n", new_df)  
输出:
Original DataFrame:      Name  Age  Salary (LPA)A  Itika   21          32.0B  Peter   26           NaNC  Harry   28          38.0D  Naill   25           NaNNew DataFrame:      Name  Age  Salary (LPA)A  Itika   21          32.0C  Harry   28          38.0 
Image
 1

Image

热门推荐

Image