Python Pandas - 选择组合的行和列的子集

要选择行和列的子集,请使用loc。使用索引运算符,即方括号并在 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

#将 CSV 文件中的数据加载到 Pandas DataFrame 中:
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesData.csv")
print("\nReading the CSV file...\n",dataFrame)

#选择行的子集
print("\nSelect cars with Units more than 100: \n",dataFrame[dataFrame["Units"] > 100])

#只显示两列
res = dataFrame[['Reg_Price','Units']];
print("\nDisplaying only two columns : \n",res)

#选择组合的行和列的子集
#右列显示您要显示的列,即此处的汽车列
res2 = dataFrame.loc[dataFrame["Units"] > 100, "Car"]

#显示子集
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

猜你喜欢