행과 열의 하위 집합을 선택하려면 loc . 대괄호와 같은 인덱스 연산자를 사용하고 위치에 조건을 설정합니다.
다음이 Microsoft Excel에서 열린 CSV 파일의 내용이라고 가정해 보겠습니다. -

먼저 CSV 파일에서 Pandas DataFrame으로 데이터를 로드합니다. -
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