栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pandas 12. conca合并

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pandas 12. conca合并

Pandas conca合并 1. 方法介绍

pandas.concat(objs, axis=0, join=‘outer’, ignore_index=False,
levels=None, names=None, verify_integrity=False, sort=False, copy=True, keys=None, )

2. 参数说明

主要介绍objs, axis, join, ignore_index四个参数

  • objs: 要合并的DataFrame/Series组成的列表
  • axis: 指定连接轴0/index或者1/column
  • join: 指定合并的方式inner/outer
  • ignore_index: 指定是否重新生成index或者column, 0,1,2…,n-1 默认为False
3. 程序实例
import pandas as pd

# df和df2的最后一列的列名不一样
df1 = pd.DataFrame({"a": ["a0", "a1", "a2"],
                    "b": ["b0", "b1", "b2"],
                    "c": ["c0", "c1", "c2"]}, index=[1, 2, 3])
df2 = pd.DataFrame({"a": ["A0", "A1", "A2"],
                    "b": ["B0", "B1", "B2"],
                    "d": ["C0", "C1", "C2"]}, index=[1, 2, 3])
ser = pd.Series([11, 22, 33], index=[1, 2, 3], name="a")

# 1. axis 指定连接轴0/index或者1/column
df3 = pd.concat([df1, ser, df2], axis=0)
# print(df3)

df4 = pd.concat([df1, ser, df2], axis=1)
# print(df4)

# 2. join 指定合并的方式inner/outer
df5 = pd.concat([df1, df2], axis=1, join="inner")  # join="inner", 如果axis=1时, 则会将两个df有相同index行合并; 如果axis=0, 则会将两个df有相同column的列合并;
# print(df5)
df6 = pd.concat([df1, df2], axis=0, join="outer")  # join="outer", 如果axis=1时, 则会将两个df有相同index行合并, 不相同的填充nan; 如果axis=0, 则会将两个df有相同column的列合并, 不相同的填充nan
# print(df6)

# 3. ignore_index 指定是否重新生成index或者column, 0,1,2..,n-1  默认为False
df7 = pd.concat([df1, df2], axis=0, ignore_index=True)  # ignore_index=True, 如果axis=1时, 则column会自动生成为0,1,2..,n-1; 如果axis=0, 则index会自动生成为0,1,2..,n-1;
# print(df7)
df8 = pd.concat([df1, df2], axis=0, ignore_index=False)  # ignore_index=False, index或者column保持原df的
# print(df8)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号