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
广告