Pandas how to concat two dataframes without losing the column headers

Question:

I have the following toy code:

 import pandas as pd
 df = pd.DataFrame()
 df["foo"] = [1,2,3,4]

 df2 = pd.DataFrame()
 df2["bar"]=[4,5,6,7]  

 df = pd.concat([df,df2], ignore_index=True,axis=1)
 print(list(df))

Output: [0,1]
Expected Output: [foo,bar] (order is not important)
Is there any way to concatenate two dataframes without losing the original column headers, if I can guarantee that the headers will be unique?
Iterating through the columns and then adding them to one of the DataFrames comes to mind, but is there a pandas function, or concat parameter that I am unaware of?

Thanks!

Asked By: Priyank

||

Answers:

As stated in merge, join, and concat documentation, ignore index will remove all name references and use a range (0…n-1) instead. So it should give you the result you want once you remove ignore_index argument or set it to false (default).

df = pd.concat([df, df2], axis=1)

This will join your df and df2 based on indexes (same indexed rows will be concatenated, if other dataframe has no member of that index it will be concatenated as nan).

If you have different indexing on your dataframes, and want to concatenate it this way. You can either create a temporary index and join on that, or set the new dataframe’s columns after using concat(…, ignore_index=True).

Answered By: umutto

I don’t think the accepted answer answers the question, which is about column headers, not indexes.

I am facing the same problem, and my workaround is to add the column names after the concatenation:

df.columns = ["foo", "bar"]
Answered By: ybenjira
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.