With header information in csv file, city can be grabbed as:
city = row['city']
Now how to assume that csv file does not have headers, there is only 1 column, and column is city.
To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.
To write DataFrame to CSV without column header (remove column names) use header=False param on to_csv() method.
You can still use your line, if you declare the headers yourself, since you know it:
with open('data.csv') as f: cf = csv.DictReader(f, fieldnames=['city']) for row in cf: print row['city']
For more information check csv.DictReader
info in the docs.
Another option is to just use positional indexing, since you know there's only one column:
with open('data.csv') as f: cf = csv.reader(f) for row in cf: print row[0]
You can use pandas.read_csv() function similarly to the way @nosklo describes, as follows:
df = pandas.read_csv("A2", header=None) print df[0]
or
df = pandas.read_csv("A2", header=None, names=(['city'])) print df['city']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With