Concatenate two columns of Pandas dataframe

Let’s discuss how to Concatenate two columns of dataframe in pandas python. We can do this by using the following functions :

  • concat()
  • append()
  • join()

Example 1 : Using the concat() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas as pd
  
# creating 2 DataFrames
location = pd.DataFrame({'area': ['new-york', 'columbo', 'mumbai']})
food = pd.DataFrame({'food': ['pizza', 'crabs', 'vada-paw']})
  
# concatenating the DataFrames
det = pd.concat([location, food], join = 'outer', axis = 1)
  
# displaying the DataFrame
print(det)

chevron_right


Output :

Example 2 : Using the append() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas as pd
  
# creating 2 DataFrames
first = pd.DataFrame([['one', 1], ['three', 3]], columns =['name', 'word'])
second = pd.DataFrame([['two', 2], ['four', 4]], columns =['name', 'word'])
  
# concatenating the DataFrames
dt = first.append(second, ignore_index = True)
  
# displaying the DataFrame
print(dt)

chevron_right


Output :



Example 3 : Using the .join() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas as pd
  
# creating 2 DataFrames
location = pd.DataFrame({'area' : ['new-york', 'columbo', 'mumbai']})
food = pd.DataFrame({'food': ['pizza', 'crabs', 'vada-paw']})
  
# concatenating the DataFrames
dt = location.join(food)
  
# displaying the DataFrame
print(dt)

chevron_right


Output :

For the three methods to concatenate two columns in a DataFrame, we can add different parameters to change the axis, sort, levels etc.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.