Python csv without header

Viewed 81145

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.

3 Answers

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']

I'm using a pandas dataframe object:

df=pd.read_sql(sql_query,data_connection)
df.to_csv(filename, header=False, index=False)

Don't know if that is the most Pythonic approach, but it gets the job done.

Related