我有一个CSV文件,我想循环一列的值(在python中)。csv文件有5000行和两列。如何循环遍历第一列而不是第二列?
I tried doing
for row in df
for column in row
但这不起作用。我怎么修理它?
我会使用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
例如,您的df看起来像这样
column1 column2
0 name1 Address1
1 name2 Adsdrees2
2 name3 Address3
访问列1
for item, row in df.iterrows():
print(row['column1'])
#打印
name1
name2
name3