zoukankan      html  css  js  c++  java
  • 连接两列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中两列的三种方法,我们可以添加不同的参数来更改轴,排序,级别等

  • 相关阅读:
    QT -- 文本文件的读写(QFile、QTextStream)
    QT -- 单例模式
    QT -- 代码封装成库给C调用的问题
    QSS -- QSS入门1
    什么是 GPU 加速?
    QT -- 文件操作 QFile
    C++ -- break和continue的区别
    C++ -- Switch的基本用法
    C++ -- ?:运算符
    OpenCV -- cv::IMREAD_GRAYSCALE 与 cv::cvtColor
  • 原文地址:https://www.cnblogs.com/a00ium/p/13874664.html
Copyright © 2011-2022 走看看