I keep seeing examples on the internet on how to import all the CSV into List/Dict/Tuple but none showing how to just import the FIRST ROW ie Header columns into a list.
I have tried doing
import csv
filename = "Example.csv"
columnHeaderList
with open(filename,'r') as data:
for line in csv.reader(data):
columnHeaderList.append(line)
break
columnHeaderList
which gave a nested list like below:
[0,
['Column 1',
'Column 2',
'Column 3']]
How do I get it like:
['Column 1', 'Column 2','Column 3']
Feel free to include answers in pandas also
Firstly open your file:-
import pandas as pd
df=pd.read_csv("Example.csv")
Note:- by default seperator is ','
if you have other seperator then pass sep
parameter in read_csv()
method
just use this:-
df.columns.to_list()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.