1

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?

2
  • 1
    if you can tweak the requirement so that column names start from Col0: df.add_prefix("Col") ? Commented Jun 5, 2020 at 13:19
  • You probably shouldn't. Column names are meant to be understandable and give information on the content of the column. If that's not the case, I'd argue it's best to leave them to their default value, which is an integer index from 0 to N-1. Commented Jun 5, 2020 at 13:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the solution, Is there a way i can make it as Col1, Col2 instead 1,2...?
@AndriyIvaneyko I suggest to generalize your solution with using df1.shape[1] for range call, and then set columns name seperatly
Thanks much. I am novice and missed the trick. :) appreciate it.
@Rohit, sure no problems, can you please mark answer as accepted if it solves your problem, so it will reflect the current state of your question :) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.