让我们讨论一下如何在pandas python中串联两列数据框。我们可以使用以下功能来做到这一点:
示例1:使用该concat()方法。
concat()
# 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)
输出:
示例2:使用该append()方法。
append()
# 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)
# 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)
对于连接DataFrame中两列的三种方法,我们可以添加不同的参数来更改轴,排序,级别等