Home > OS >  How to add column name from the pd.concat?
How to add column name from the pd.concat?

Time:07-18

I am wondering is there any way to add column name from series of data.

I thought is something like this

std_df = pd.concat([q,w], axis=1, columns= ["apple","banana"])

But I get an error of concat() got an unexpected keyword argument 'columns'

I've tried names instead of columns, still not working.

CodePudding user response:

pd.concat() has the keys parameter that will work for this purpose:

s1 = pd.Series([1,2,3])
s2 = pd.Series([4,5,6])

df = pd.concat([s1,s2],keys = ['A','B'],axis=1)

Output:

   A  B
0  1  4
1  2  5
2  3  6

CodePudding user response:

Looks like you are trying to set the axis labels?

pd.concat([pd.Series([1,2]),pd.Series([3,4])],axis=1).set_axis(labels=['A','B'],axis=1)

Output

   A  B
0  1  3
1  2  4

CodePudding user response:

I believe pd.concat() has no parameter columns. You should delete that.

std_df = pd.concat([q,w], axis=1)
  • Related