【问题标题】:How to store data of a specific column from a csv file to a list in Python如何将特定列的数据从 csv 文件存储到 Python 中的列表
【发布时间】:2021-06-01 09:42:54
【问题描述】:
【问题讨论】:
标签:
python
python-3.x
scripting
【解决方案1】:
一种方法是使用pandas。
import pandas as pd
df = pd.read_csv('filepath_here')
your_list = df['Name'].tolist()
【解决方案2】:
最简单的方法是使用 pandas:
import pandas as pd
df = pd.read_csv('names.csv')
names = df['Name'].tolist()
【解决方案3】:
一种可能性是使用list comprehension。
import csv
with open("names.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
# List comprehension
csv_list = [line[0] for line in csv_reader]
line[0] 中的“0”可以更改为所需的任何列。