1. 程式人生 > 實用技巧 >連線兩列Pandas資料框

連線兩列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)

輸出:

示例3:使用.join()方法。
# 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中兩列的三種方法,我們可以新增不同的引數來更改軸,排序,級別等