-1

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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Nikhita
  • 1
  • 1
  • Transpose it - [How to transpose a dataset in a csv file?](https://stackoverflow.com/questions/4869189/how-to-transpose-a-dataset-in-a-csv-file) – wwii Dec 12 '22 at 15:30
  • Your title says row, shouldn't it say column? – Barmar Dec 12 '22 at 15:45
  • If you only want one column, why are you looping through all the columns? You seem to have made the same mistake in the code as you did in the title. – Barmar Dec 12 '22 at 15:46

2 Answers2

0

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
JJ Asghar
  • 614
  • 5
  • 15
0

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
  • Why not just `for val in df['column1']`? it will be much faster – DeepSpace Dec 12 '22 at 15:42
  • Agree @DeepSpace using `for val in df['column1']` snippet changes dtypes for some cases. To preserve `dtype ` and safly itterrate `df.iterrows()` is recommonded sometimes [Ref](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html) check last 2 lines – Bhargav - Retarded Skills Dec 12 '22 at 15:50
  • 1
    That reference only compares `iterrows` to `itertuples`. Anyway, OP didn't mention any importance to dtypes, so I'd at least mention the much faster approach – DeepSpace Dec 12 '22 at 15:53