Pattara
Pattara

Reputation: 45

How to create column names in pandas dataframe?

I have exported the gold price data from the brokers. the file has no column names like this

2014.02.13,00:00,1291.00,1302.90,1286.20,1302.30,41906 2014.02.14,00:00,1301.80,1321.20,1299.80,1318.70,46244 2014.02.17,00:00,1318.20,1329.80,1318.10,1328.60,26811 2014.02.18,00:00,1328.60,1332.10,1312.60,1321.40,46226

I read csv to pandas dataframe and it take the first row to be the column names. I am curious how can I set the column names and still have all the data

Thank you

Upvotes: 3

Views: 24981

Answers (1)

Sanidhya Singh
Sanidhya Singh

Reputation: 521

if you don't have a header in the CSV, you can instruct Pandas to ignore it using the header parameter

df = pd.read_csv(file_path, header=None)

to manually assign column names to the DataFrame, you may use

df.columns = ["col1", "col2", ...]

Encourage you to go through the read_csv documentation to learn more about the options it provides.

Upvotes: 10

Related Questions