Rohit
Rohit

Reputation: 97

how to add column name in a dataframe created from a csv file?

I have Hive db table csv extract which has no header. I loaded csv as dataframe andit has no column name. Since length of column will change as per datatable, how can I assign col name according to column length?

i know the way to assign column for fixed column length.

>>> df1 = pd.read_csv('/home/j/HiveOP_06June_1.csv', header = None)
>>> df1.columns = ['Col1','Col2', 'Col3']
>>> df1
   Col1 Col2                 Col3
0  XPRN    A  2019-12-16 00:00:00

If I am exporting data table with 25 columns then how can i name all col on the fly?

Upvotes: 1

Views: 328

Answers (1)

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22031

I would do it like this:

names = [('Col' + str(i)) for i in range(1, 26)]
df1 = pd.read_csv('/home/j/HiveOP_06June_1.csv', names=names, header=None)

Of course, you can name your columns manually with name for each of them.

Upvotes: 2

Related Questions