简体   繁体   中英

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?

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM