连接两列Pandas数据框

 

让我们讨论一下如何在pandas python中串联两列数据框。我们可以使用以下功能来做到这一点:

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

示例1:使用该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()方法。

# 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中两列的三种方法,我们可以添加不同的参数来更改轴,排序,级别等

posted @ 2020-10-25 19:16  DaisyLinux  阅读(915)  评论(0编辑  收藏  举报