____tz_zs

官网相关介绍:

https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.concat.html

https://pandas.pydata.org/pandas-docs/stable/merging.html#

.

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-"""
@author:    tz_zs
"""import numpy as np
import pandas as pddata1 = [[11, 12, 13, 14, 15], [21, 22, 23, 24, 25], [31, 32, 33, 34, 35], [41, 42, 43, 44, 45]]
date_range1 = pd.date_range(start="20180701", periods=4)
df = pd.DataFrame(data=data1, index=date_range1,columns=['a', 'b', 'c', 'd', 'e'])
print df
"""a   b   c   d   e
2018-07-01  11  12  13  14  15
2018-07-02  21  22  23  24  25
2018-07-03  31  32  33  34  35
2018-07-04  41  42  43  44  45
"""data2 = [1, 2, 3, 4]
date_range2 = pd.date_range(start="20180702", periods=4)
df2 = pd.Series(data=data2, index=date_range2, name='f')
print df2
"""
2018-07-02    1
2018-07-03    2
2018-07-04    3
2018-07-05    4
Freq: D, Name: f, dtype: int64
"""data3 = [['1e', '1g', '1h'], ['2e', '2g', '2h'], ['3e', '3g', '3h'], ['4e', '4g', '4h']]
date_range3 = pd.date_range(start="20180703", periods=4)
df3 = pd.DataFrame(data=data3, index=date_range3, columns=['e', '3g', '3h'])
print df3
"""e  3g  3h
2018-07-03  1e  1g  1h
2018-07-04  2e  2g  2h
2018-07-05  3e  3g  3h
2018-07-06  4e  4g  4h
"""

.

方法一:将 Series 或 df 的一列直接赋给原始 df 作为一列

此种用法 pandas 会自动将插入的数据对齐到原始 df 的 index ,缺失值的地方为 NaN,index 之外的值将舍弃掉。


df["f"] = df2
print df
"""a   b   c   d   e    f
2018-07-01  11  12  13  14  15  NaN
2018-07-02  21  22  23  24  25  1.0
2018-07-03  31  32  33  34  35  2.0
2018-07-04  41  42  43  44  45  3.0
"""df['3g'] = df3['3g']
df['3h'] = df3['3h']
print df
"""a   b   c   d   e    f   3g   3h
2018-07-01  11  12  13  14  15  NaN  NaN  NaN
2018-07-02  21  22  23  24  25  1.0  NaN  NaN
2018-07-03  31  32  33  34  35  2.0   1g   1h
2018-07-04  41  42  43  44  45  3.0   2g   2h
"""

方法二:pd.concat

此种方法,将完整的保留数据(不会丢弃数据,而是根据 index 和 columns 增加行列),并可指定合并的轴。

官网 pandas.concat

pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
          keys=None, levels=None, names=None, verify_integrity=False,
          copy=True)

参数 axis 默认为0

df4 = pd.concat([df, df2, df3], axis=0)
print df4
"""0     a     b     c     d    e   3g   3h
2018-07-01  NaN  11.0  12.0  13.0  14.0   15  NaN  NaN
2018-07-02  NaN  21.0  22.0  23.0  24.0   25  NaN  NaN
2018-07-03  NaN  31.0  32.0  33.0  34.0   35  NaN  NaN
2018-07-04  NaN  41.0  42.0  43.0  44.0   45  NaN  NaN
2018-07-02  1.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
2018-07-03  2.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
2018-07-04  3.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
2018-07-05  4.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
2018-07-03  NaN   NaN   NaN   NaN   NaN   1e   1g   1h
2018-07-04  NaN   NaN   NaN   NaN   NaN   2e   2g   2h
2018-07-05  NaN   NaN   NaN   NaN   NaN   3e   3g   3h
2018-07-06  NaN   NaN   NaN   NaN   NaN   4e   4g   4h
"""df4 = pd.concat([df, df2, df3], axis=1)
print df4
"""a     b     c     d     e    f    e   3g   3h
2018-07-01  11.0  12.0  13.0  14.0  15.0  NaN  NaN  NaN  NaN
2018-07-02  21.0  22.0  23.0  24.0  25.0  1.0  NaN  NaN  NaN
2018-07-03  31.0  32.0  33.0  34.0  35.0  2.0   1e   1g   1h
2018-07-04  41.0  42.0  43.0  44.0  45.0  3.0   2e   2g   2h
2018-07-05   NaN   NaN   NaN   NaN   NaN  4.0   3e   3g   3h
2018-07-06   NaN   NaN   NaN   NaN   NaN  NaN   4e   4g   4h
"""

.

参数 join 默认为 join='outer'。如果改为 inner 则得到的是表的交集

# join 默认为 join='outer'。如果改为 inner 则得到的是表的交集
df6 = pd.concat([df, df2, df3], axis=1, join='inner')
print df5
"""a   b   c   d   e  f   e  3g  3h
2018-07-03  31  32  33  34  35  2  1e  1g  1h
2018-07-04  41  42  43  44  45  3  2e  2g  2h
"""

.

参数 ignore_index 默认为 False ,当设为 ignore_index=True 时,新 df 将不会使用拼接成员 df 的 index,而是重新生成一个从 0 开始的 index 值。

df5 = pd.concat([df, df2, df3], axis=1,ignore_index=True)
print df5
"""0     1     2     3     4    5    6    7    8
2018-07-01  11.0  12.0  13.0  14.0  15.0  NaN  NaN  NaN  NaN
2018-07-02  21.0  22.0  23.0  24.0  25.0  1.0  NaN  NaN  NaN
2018-07-03  31.0  32.0  33.0  34.0  35.0  2.0   1e   1g   1h
2018-07-04  41.0  42.0  43.0  44.0  45.0  3.0   2e   2g   2h
2018-07-05   NaN   NaN   NaN   NaN   NaN  4.0   3e   3g   3h
2018-07-06   NaN   NaN   NaN   NaN   NaN  NaN   4e   4g   4h
"""
df5 = pd.concat([df, df2, df3], axis=0,ignore_index=True)
print df5
"""0     a     b     c     d    e   3g   3h
0   NaN  11.0  12.0  13.0  14.0   15  NaN  NaN
1   NaN  21.0  22.0  23.0  24.0   25  NaN  NaN
2   NaN  31.0  32.0  33.0  34.0   35  NaN  NaN
3   NaN  41.0  42.0  43.0  44.0   45  NaN  NaN
4   1.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
5   2.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
6   3.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
7   4.0   NaN   NaN   NaN   NaN  NaN  NaN  NaN
8   NaN   NaN   NaN   NaN   NaN   1e   1g   1h
9   NaN   NaN   NaN   NaN   NaN   2e   2g   2h
10  NaN   NaN   NaN   NaN   NaN   3e   3g   3h
11  NaN   NaN   NaN   NaN   NaN   4e   4g   4h
"""

.

参数 join_axes 可指定 index 来对齐数据。这样会切掉指定的 index 之外的数据。

df5 = pd.concat([df, df2, df3], axis=1, join_axes=[df.index])
print df5
"""a   b   c   d   e    f    e   3g   3h
2018-07-01  11  12  13  14  15  NaN  NaN  NaN  NaN
2018-07-02  21  22  23  24  25  1.0  NaN  NaN  NaN
2018-07-03  31  32  33  34  35  2.0   1e   1g   1h
2018-07-04  41  42  43  44  45  3.0   2e   2g   2h
"""df5 = pd.concat([df, df2, df3], axis=1, join_axes=[df2.index])
print df5
"""a     b     c     d     e  f    e   3g   3h
2018-07-02  21.0  22.0  23.0  24.0  25.0  1  NaN  NaN  NaN
2018-07-03  31.0  32.0  33.0  34.0  35.0  2   1e   1g   1h
2018-07-04  41.0  42.0  43.0  44.0  45.0  3   2e   2g   2h
2018-07-05   NaN   NaN   NaN   NaN   NaN  4   3e   3g   3h
"""df5 = pd.concat([df, df2, df3], axis=1, join_axes=[df3.index])
print df5
"""a     b     c     d     e    f   e  3g  3h
2018-07-03  31.0  32.0  33.0  34.0  35.0  2.0  1e  1g  1h
2018-07-04  41.0  42.0  43.0  44.0  45.0  3.0  2e  2g  2h
2018-07-05   NaN   NaN   NaN   NaN   NaN  4.0  3e  3g  3h
2018-07-06   NaN   NaN   NaN   NaN   NaN  NaN  4e  4g  4h
"""df5 = pd.concat([df, df2, df3], axis=1, join_axes=[pd.date_range(start="20180704", periods=4)])
print df5
"""a     b     c     d     e    f    e   3g   3h
2018-07-04  41.0  42.0  43.0  44.0  45.0  3.0   2e   2g   2h
2018-07-05   NaN   NaN   NaN   NaN   NaN  4.0   3e   3g   3h
2018-07-06   NaN   NaN   NaN   NaN   NaN  NaN   4e   4g   4h
2018-07-07   NaN   NaN   NaN   NaN   NaN  NaN  NaN  NaN  NaN
"""

.

方法三:df1.append(df2)

将被 append 的对象添加到调用者的末尾(类似 list 的方法)。

注意:如果调用者是 DataFrame 而被 append 的对象是 Series,将会把 Series 作为一行添加到末尾。

Series 和 DataFrame 均有此方法。

df_append_df3 = df.append(df3)
print df_append_df3
"""3g   3h     a     b     c     d   e
2018-07-01  NaN  NaN  11.0  12.0  13.0  14.0  15
2018-07-02  NaN  NaN  21.0  22.0  23.0  24.0  25
2018-07-03  NaN  NaN  31.0  32.0  33.0  34.0  35
2018-07-04  NaN  NaN  41.0  42.0  43.0  44.0  45
2018-07-03   1g   1h   NaN   NaN   NaN   NaN  1e
2018-07-04   2g   2h   NaN   NaN   NaN   NaN  2e
2018-07-05   3g   3h   NaN   NaN   NaN   NaN  3e
2018-07-06   4g   4h   NaN   NaN   NaN   NaN  4e
"""df2_append_df3 = df2.append(df3)
print df2_append_df3
"""0   3g   3h    e
2018-07-02  1.0  NaN  NaN  NaN
2018-07-03  2.0  NaN  NaN  NaN
2018-07-04  3.0  NaN  NaN  NaN
2018-07-05  4.0  NaN  NaN  NaN
2018-07-03  NaN   1g   1h   1e
2018-07-04  NaN   2g   2h   2e
2018-07-05  NaN   3g   3h   3e
2018-07-06  NaN   4g   4h   4e
"""

.

end

【pandas】 之 Series、DataFrame 的拼接 —— pd.concat、df.append(df)相关推荐

  1. dataframe两个表合并_Part25:Pandas基础(Series,DataFrame类的创建、索引、切片、算术方法)...

    一.为什么学习pandas numpy已经可以帮助我们进行数据的处理了,那么学习pandas的目的是什么呢? numpy能够帮助我们处理的是数值型的数据,当然在数据分析中除了数值型的数据还有好多其他类 ...

  2. python数据拼接: pd.concat

    1.concat concat函数是在pandas底下的方法,可以将数据根据不同的轴作简单的融合 pd.concat(objs, axis=0, join='outer', join_axes=Non ...

  3. pandas数据合并与重塑(pd.concat篇)

    1 concat concat函数是在pandas底下的方法,可以将数据根据不同的轴作简单的融合 pd.concat(objs, axis=0, join='outer', join_axes=Non ...

  4. 【DS with Python】 Pandas中Series DataFrame的结构、创建、查询、修改语法与实例

    文章目录 前言 一.Series结构与应用 1.1 Series的构造 1.2 创建Series 1.2.1 可用于创建Series的类型 1.2.2 三种设置index的方法 1.2.3 Serie ...

  5. Pandas 中 Series 和 DataFrame 知识点

    Series Series对象的创建 # pandas 学习 import pandas as pd from pandas import Series,DataFrame import numpy ...

  6. dataframe python格式_python3.6 pandas,Series和DataFrame基础格式与用法,附代码实例

    pandas 是基于numpy构建的库,加上numpy,主要用于科学运算和数据处理. 也是一个让我忘记昂贵的MATLAB,并且不得不复习SQL的库.. 一般引入规定: In [105]: from p ...

  7. pandas数据结构:Series/DataFrame;python函数:range/arange

    1. Series Series 是一个类数组的数据结构,同时带有标签(lable)或者说索引(index). 1.1 下边生成一个最简单的Series对象,因为没有给Series指定索引,所以此时会 ...

  8. pandas Series DataFrame 丢弃指定轴上的项(三)

    1.0 删除Series项 from pandas import Series,DataFrame import numpy as np import pandas as pd obj=Series( ...

  9. python科学计算笔记(三)pandas中Series和DataFrame练习

    from pandas import Series, DataFrame# Series接收list或dict作为一维数据 #两个属性:values, index #① s1 = Series([4, ...

  10. Python数据处理中 pd.concat 与 pd.merge 区别

    背景 数据的合并与关联是数据处理过程中经常遇到的问题,在SQL.HQL中大家可能都有用到 join.uion all 等 ,在 Pandas 中也有同样的功能,来满足数据处理需求,个人感觉Pandas ...

最新文章

  1. Go Pro 半小时上手指南
  2. 定义Serializer序列化器
  3. [推荐]在JavaScript中实现命名空间
  4. 一种可以穿透还原卡和还原软件的代码
  5. 雅客EXCEL(6)-通用表格格式、销售实际案例(总结之前的知识点)
  6. 友盟2015年Q2、Q3中国移动互联网趋势报告
  7. FireFox IE Opera Safari 都可以正常播放WMV和MOV的网页播放器代码
  8. java生成验证码工具类_Java生成图形验证码工具类
  9. Java并发编程—为什么 wait() 方法需要写在 while 里,而不是 if?
  10. java.io.StreamCorruptedException: invalid type code: AC解决办法
  11. 对vector/string执行insert/erase操作后迭代器的情况说明
  12. 深入浅出数据分析(一)——MySQL+EXCEL+R统计问卷调查
  13. TOGAF9企业架构规划与设计学习考试经验简记
  14. Google 最新版 Chrome 崩溃
  15. JSP设置网站favicon.ico
  16. python面向对象游戏_【Python之旅】第四篇(四):基于面向对象的模拟人生游戏类...
  17. 关于魔兽守卫军的改进建议
  18. KSO-Sqlserver事务的实现
  19. IntelliJ IDEA项目正常编译,但是代码部分飘红
  20. 学习四旋翼(三):DMP姿态解算和串级PID控制姿态

热门文章

  1. ppt设置外观样式_幻灯片的外观设置
  2. 查看WIN10密钥备忘
  3. 紫猫插件php,简易中控紫猫插件版(3)压缩包使用说明
  4. Google广告数据分析与优化总结
  5. css固定图片大小 vue_img设置图片大小 vue_如何改变图片大小
  6. 华为狼性文化遭质疑,那我们当个佛系程序员可好?
  7. MXF Operational Pattern 1a (OP1a)
  8. 李迅雷:大城市化和居民加杠杆能支撑房价多久
  9. 北京市电动自行车产品目录 汇总查询
  10. MarkDown--- 让CSDN的博客更炫丽,添加小图标,调整字体大小和颜色