【问题标题】:How do I loop through one row of a CSV file in Python?如何在 Python 中循环遍历 CSV 文件的一行?
【发布时间】:2022-12-12 23:36:19
【问题描述】:

我有一个 CSV 文件,我想遍历一列的值(在 python 中)。 csv 文件有 5000 行和两列。如何只遍历第一列而不遍历第二列?

我试着做 for row in df for column in row

但这没有用。我如何解决它?

【问题讨论】:

标签: python csv


【解决方案1】:

我会使用 pandas 来做这个……就像这样:

import pandas as pd
df = pd.read_csv('blah.csv')
column = df['column_name'] # column_name is the name of the column
for row in column:
    print row

【讨论】:

    【解决方案2】:
    for item, row in df.iterrows():
        print(row['column1'])
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2018-12-01
      • 2020-06-25
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多