Pandas DataFrame 排除列

Suraj Joshi 2023年1月30日
  1. Pandas 使用 loc 属性选择除一列以外的所有列
  2. Pandas 使用 drop() 方法选择除一列以外的所有列
  3. Pandas 使用 difference() 方法选择除一列外的所有列
Pandas DataFrame 排除列

本教程解释了我们如何从一个 DataFrame 中选择除一列以外的所有列。我们将在本文中使用下面的 DataFrame 示例。

import pandas as pd

stocks_df = pd.DataFrame(
    {
        "Stock": ["Amazon", "Tesla", "Facebook", "Boeing"],
        "Price(in $)": [3180, 835, 267, 209],
        "Sector": ["Technology", "Technology", "Technology", "Aircraft"],
    }
)

print("Stocks Dataframe:")
print(stocks_df, "\n")

输出:

Stocks Dataframe:
      Stock  Price(in $)      Sector
0    Amazon         3180  Technology
1     Tesla          835  Technology
2  Facebook          267  Technology
3    Boeing          209    Aircraft

Pandas 使用 loc 属性选择除一列以外的所有列

import pandas as pd

stocks_df = pd.DataFrame(
    {
        "Stock": ["Amazon", "Tesla", "Facebook", "Boeing"],
        "Price(in $)": [3180, 835, 267, 209],
        "Sector": ["Technology", "Technology", "Technology", "Aircraft"],
    }
)

print("Stocks Dataframe:")
print(stocks_df, "\n")

print("Stocks DataFrame excluding Sector Column:")
filtered_df = stocks_df.loc[:, stocks_df.columns != "Sector"]
print(filtered_df, "\n")

输出:

Stocks Dataframe:
      Stock  Price(in $)      Sector
0    Amazon         3180  Technology
1     Tesla          835  Technology
2  Facebook          267  Technology
3    Boeing          209    Aircraft

Stocks DataFrame excluding Sector Column:
      Stock  Price(in $)
0    Amazon         3180
1     Tesla          835
2  Facebook          267
3    Boeing          209

它从 DataFrame stocks_df 中选择除 Sector 列以外的所有元素,将结果分配给 filtered_df,然后显示 filetered_df 的内容。

loc 属性根据指定的行和列选择元素。loc 属性中:符号在 , 前指定我们需要选择所有的行。对于列,我们指定只选择名称不是 Sector 的列。因此,它将选择除 Sector 列以外的所有列。

Pandas 使用 drop() 方法选择除一列以外的所有列

我们可以使用 drop() 方法,通过在方法中设置 axis=1,从 DataFrame 中删除指定的列。

import pandas as pd

stocks_df = pd.DataFrame(
    {
        "Stock": ["Amazon", "Tesla", "Facebook", "Boeing"],
        "Price(in $)": [3180, 835, 267, 209],
        "Sector": ["Technology", "Technology", "Technology", "Aircraft"],
    }
)

print("Stocks Dataframe:")
print(stocks_df, "\n")

print("Stocks DataFrame excluding Sector Column:")
filtered_df = stocks_df.drop("Sector", axis=1)
print(filtered_df, "\n")

输出:

Stocks Dataframe:
      Stock  Price(in $)      Sector
0    Amazon         3180  Technology
1     Tesla          835  Technology
2  Facebook          267  Technology
3    Boeing          209    Aircraft

Stocks DataFrame excluding Sector Column:
      Stock  Price(in $)
0    Amazon         3180
1     Tesla          835
2  Facebook          267
3    Boeing          209

它从 stocks_df DataFrame 中删除 Sector 列,并将结果分配给 filtered_df

我们也可以通过使用 drop() 方法从 DataFrame 中删除多个列。我们提供一个列名列表作为 drop() 方法的参数。

import pandas as pd

stocks_df = pd.DataFrame(
    {
        "Stock": ["Amazon", "Tesla", "Facebook", "Boeing"],
        "Price(in $)": [3180, 835, 267, 209],
        "Sector": ["Technology", "Technology", "Technology", "Aircraft"],
    }
)

print("Stocks Dataframe:")
print(stocks_df, "\n")

print("Stocks DataFrame excluding Sector and Price Column:")
filtered_df = stocks_df.drop(["Sector", "Price(in $)"], axis=1)
print(filtered_df, "\n")

输出:

Stocks Dataframe:
      Stock  Price(in $)      Sector
0    Amazon         3180  Technology
1     Tesla          835  Technology
2  Facebook          267  Technology
3    Boeing          209    Aircraft

Stocks DataFrame excluding Sector and Price Column:
      Stock
0    Amazon
1     Tesla
2  Facebook
3    Boeing

它从 stocks_df DataFrame 中排除了 Price(in $)Sector 列。

Pandas 使用 difference() 方法选择除一列外的所有列

import pandas as pd

stocks_df = pd.DataFrame(
    {
        "Stock": ["Amazon", "Tesla", "Facebook", "Boeing"],
        "Price(in $)": [3180, 835, 267, 209],
        "Sector": ["Technology", "Technology", "Technology", "Aircraft"],
    }
)

print("Stocks Dataframe:")
print(stocks_df, "\n")

print("Stocks DataFrame excluding Sector Column:")
filtered_df = stocks_df[stocks_df.columns.difference(["Sector"])]
print(filtered_df, "\n")

输出:

Stocks Dataframe:
      Stock  Price(in $)      Sector
0    Amazon         3180  Technology
1     Tesla          835  Technology
2  Facebook          267  Technology
3    Boeing          209    Aircraft

Stocks DataFrame excluding Sector Column:
   Price(in $)     Stock
0         3180    Amazon
1          835     Tesla
2          267  Facebook
3          209    Boeing

它从 stocks_df DataFrame 中删除 Sector 列,并将结果分配给 filtered_df

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame Column

相关文章 - Pandas Filter