Python Pandas - 結合選擇的行和列子集
若要選擇行和列子集,請使用 loc。使用索引運算子,即方括號,並在 loc 中設定條件。
假設 Microsoft Excel 中開啟的 CSV 檔案的內容如下 −

首先,將資料從 CSV 檔案載入到 Pandas 資料框中 −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")結合選擇行和列子集。右側列顯示要顯示的列,此處為汽車列 −
dataFrame.loc[dataFrame["Units"] > 100, "Car"]
示例
程式碼如下 −
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)
# selecting a subset of rows
print("\nSelect cars with Units more than 100: \n",dataFrame[dataFrame["Units"] > 100])
# displaying only two columns
res = dataFrame[['Reg_Price','Units']];
print("\nDisplaying only two columns : \n",res)
# Select a subset of rows and columns combined
# Right column displays the column you want to display i.e. Cars column here
res2 = dataFrame.loc[dataFrame["Units"] > 100, "Car"]
# display subset
print("\nSubset...\n",res2)輸出
這將生成以下輸出 −
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 Select cars with Units more than 100: Car Reg_Price Units 2 Audi 2500 120 4 Mustang 2500 110 Displaying only two columns : Reg_Price Units 0 2500 100 1 3500 80 2 2500 120 3 2000 70 4 2500 110 Subset... 2 Audi 4 Mustang Name: Car, dtype: object
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP