Let's discuss how to Concatenate two columns of dataframe in pandas python. We can do this by using the following functions :
Python3 1==
Output :
Example 2 : Using the
Python3 1==
Output :
Example 3 : Using the .join() method.
Python3 1==
- concat()
- append()
- join()
concat() method.
# 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)
Example 2 : Using the append() method.
# 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)
Example 3 : Using the .join() method.
# 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)
Output :
For the three methods to concatenate two columns in a DataFrame, we can add different parameters to change the axis, sort, levels etc.
For the three methods to concatenate two columns in a DataFrame, we can add different parameters to change the axis, sort, levels etc.