Home > Software engineering >  How do I loop through one row of a CSV file in Python?
How do I loop through one row of a CSV file in Python?

Time:12-13

I have a CSV File, and I want to loop through the values of one column (in python). The csv file has 5000 rows and two columns. How do I loop through only the first column and not the second?

I tried doing

for row in df
for column in row

But this didn't work. How do I fix it?

CodePudding user response:

I'd use pandas to do this...something like:

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

CodePudding user response:

For eaxmple your df looks like this

  column1    column2
0   name1   Address1
1   name2  Adsdrees2
2   name3   Address3

To access column1

for item, row in df.iterrows():
    print(row['column1'])

prints #

name1
name2
name3
  • Related