Carlos Carvalho
Carlos Carvalho

Reputation: 107

Pandas - get list df columns names (e.g) in a string

I've the following dataframe

dtf = pd.DataFrame({'col1' : ['howdy_dude'],
             'col2' : ["HI"],
             'col3' : ["3"]})

I need to get just the header columns with output in string format,
kind of : 'col1 + col2 + col3'

3 columns for this sample, but sometimes the number of columns can be higher, sometimes lower.

Thank you.

Upvotes: 1

Views: 2275

Answers (4)

Michaël
Michaël

Reputation: 201

The exact answer is, as mentioned by Harry_pb's answer:

" + ".join(dtf.columns)

Note that (list()) is useless.


However, it won't work if your column names are integers. You need to first convert them into strings, for example:

dtf.columns = [1,2,3]
" + ".join( dtf.columns.astype(str) )

Also, this method won't work if you have a MultiIndex. So in general, and it's quicker to write, you can do:

" + ".join( dtf.columns.format() )

If you need more control on the MultIndex format, I would use a list comprehension. For a fancy example:

id = pd.MultiIndex.from_tuples( (('A','X',0), ('B','Y',0), ('C','X',0)) )

'\n'.join([
    str(level) if i == 0 else '{}|_{}'.format('  '*(i-1), level)
    for elmt in id
    for i, level in enumerate(elmt)
])

Out:

A
|_X
  |_0
B
|_Y
  |_0
C
|_X
  |_0

Upvotes: 1

Rajat Jain
Rajat Jain

Reputation: 2032

If specifically needed in this 'col1 + col2 + col3' format then,

"+".join(list(dtf.columns))

Upvotes: 0

Hari_pb
Hari_pb

Reputation: 7416

As I understood your question, you want sum of all column strings irrespective of the number of columns.

Here is response

dtf = pd.DataFrame({'col1' : ['howdy_dude'],
             'col2' : ["HI"],
             'col3' : ["3"]})

dtf['new'] = dtf.apply(' '.join, axis=1)
dtf

This new column will have sum of all the strings in all the columns for the given row (you may remove space in join if you want).

enter image description here

And, if you want to add column names as string, you can use join as

dtf = pd.DataFrame({'col1' : ['howdy_dude'],
             'col2' : ["HI"],
             'col3' : ["3"]})

result = " ".join(dtf.columns)
print (type(result))
result 

enter image description here

Hope this helps

Upvotes: 2

Lior T
Lior T

Reputation: 137

to get all the columns use:

dtf.columns.tolist()

then you have a list of them and you can concatonate them as you like.

Upvotes: -1

Related Questions