4949澳门免费精准绝技
要在Python中从CSV文件中提取特定的列,你可以使用CSV模块或者Pandas库。下面我将为你提供两种方法的示例代码:
方法一:使用CSV模块
import csv # 打开CSV文件 with open('data.csv', 'r') as file: reader = csv.reader(file) # 提取的列索引 column_indices = [0, 2, 4] # 假设你要提取第1新澳2025最新资料大全藏宝图 、第3和第5列 # 逐行读取CSV数据,并提取指定的列 for row in reader: selected_columns = [row[i] for i in column_indices] print(selected_columns)
方法二:使用Pandas库
import pandas as pd # 读取CSV文件 df = pd.read_csv('data.csv') # 提取指定的列 selected_columns = df.iloc[:, [0, 2, 4]] # 假设你要提取第1、第3和第5列 # 打印提取的列 print(selected_columns)
以上代码中,你需要将文件名 替换为你实际使用的CSV文件名。在示例代码中,我们假设要提取的列的索引是 ,你可以根据自己的需求修改为所需的列索引。