zoukankan      html  css  js  c++  java
  • pd.concat

    import numpy as np
    import pandas as pd
    df1=pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'])
    df2=pd.DataFrame(np.ones((3,4))*1,columns=['a','b','c','d'])
    df3=pd.DataFrame(np.ones((3,4))*2,columns=['a','b','c','d'])
         a    b    c    d
    0  0.0  0.0  0.0  0.0
    1  0.0  0.0  0.0  0.0
    2  0.0  0.0  0.0  0.0
         a    b    c    d
    0  1.0  1.0  1.0  1.0
    1  1.0  1.0  1.0  1.0
    2  1.0  1.0  1.0  1.0
         a    b    c    d
    0  2.0  2.0  2.0  2.0
    1  2.0  2.0  2.0  2.0
    2  2.0  2.0  2.0  2.0
    frame1 = pd.concat([df1,df2,df3],axis=0)#0为纵向,1为横向   一般合并都是选取1
    print(frame1)
    pd.concat([df1,df2],axis=1)  #进行纵向合并
    abcdabcd
    0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
    1 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
    2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
    res=pd.concat([df1,df2,df3],axis=0)#0为纵向,1为横向 
    print(res)

    a b c d
    0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0 1.0 1.0 1.0 1.0 1 1.0 1.0 1.0 1.0 2 1.0 1.0 1.0 1.0 0 2.0 2.0 2.0 2.0 1 2.0 2.0 2.0 2.0 2 2.0 2.0 2.0 2.0
    但是上面的结果有索引重复,去除索引重复可以采用ignore_index属性
    
    res=pd.concat([df1,df2,df3],axis=0,ignore_index=True)#0为纵向,1为横向
    print(res)
         a    b    c    d
    0  0.0  0.0  0.0  0.0
    1  0.0  0.0  0.0  0.0
    2  0.0  0.0  0.0  0.0
    3  1.0  1.0  1.0  1.0
    4  1.0  1.0  1.0  1.0
    5  1.0  1.0  1.0  1.0
    6  2.0  2.0  2.0  2.0
    7  2.0  2.0  2.0  2.0
    8  2.0  2.0  2.0  2.0
  • 相关阅读:
    leetcode 2 Add Two Numbers
    leetcode1
    二叉树的最大高度和最大宽度
    插入排序
    eventEmitter学习
    用node.js做一个爬虫
    HTLM5 WebSocket权威指南
    (new Function("return " + json))();
    JS中的this变量的使用介绍
    node.js 模块加载原理
  • 原文地址:https://www.cnblogs.com/zhange000/p/10384373.html
Copyright © 2011-2022 走看看