Pandas DataFrame 排除列
Suraj Joshi
2023年1月30日
2021年1月22日
本教程解釋了我們如何從一個 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
。
Author: Suraj Joshi
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn相關文章 - Pandas DataFrame Column
- 如何將 Pandas DataFrame 列標題獲取為列表
- 如何刪除 Pandas DataFrame 列
- 如何在 Pandas 中將 DataFrame 列轉換為日期時間
- 如何獲得 Pandas 列中元素總和
- 如何更改 Panas DataFrame 列的順序
- 如何在 Pandas 中將 DataFrame 列轉換為字串