在pandas中,使用 difference() 方法选择除一列外的所有列[每日解析]

在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。

要回复问题请先登录注册