Python - 從 Pandas 資料框中選擇多列
假設在 Microsoft Excel 中開啟的 CSV 檔案包含以下內容 −
首先,將資料從 CSV 檔案載入到 Pandas DataFrame 中 −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
要選擇多個列記錄,請使用方括號。在方括號中提到列,並從整個資料集中獲取多列 −
dataFrame[['Reg_Price','Units']]
示例
以下是程式碼 −
import pandas as pd # Load data from a CSV file into a Pandas DataFrame: dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("\nReading the CSV file...\n",dataFrame) # displaying two columns res = dataFrame[['Reg_Price','Units']]; print("\nDisplaying two columns : \n",res)
輸出
這將產生以下輸出 −
Reading the CSV file... Car Reg_Price Units 0 BMW 2500 100 1 Lexus 3500 80 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110 Displaying two columns : Reg_Price Units 0 2500 100 1 3500 80 2 2500 120 3 2000 70 4 2500 110
廣告