关于Pandas的基本用法_05

# 数据存储和调用
import numpy as np
import pandas as pd
# data = pd.read_csv('E:\pythonDemo\Pandas\data\data.csv')
# # 读取csv文件
# data.to_pickle('E:\pythonDemo\Pandas\data\data.pickle')
# # 储存文件
df1 = pd.DataFrame(np.arange(12).reshape((3, 4)), columns=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.arange(12,24).reshape((3,4)), columns=['a', 'b', 'c', 'd'])
df3 = pd.DataFrame(np.arange(24,36).reshape((3,4)), columns=['a', 'b', 'c', 'd'])
df4 = pd.DataFrame(np.arange(36,48).reshape((3,4)), index=[2, 3, 4], columns=['b', 'c', 'd', 'e'])
res = pd.concat([df1, df2, df3],axis=0,ignore_index=True)
print(res)
# 合并,通过axis控制合并类型,默认为纵向合并,ignore_index参数为True是重新建立索引防止索引重复产生问题
res = pd.concat([df1, df4], axis=0, ignore_index=True, join='inner')
# 在两个矩阵中存在部分不同的索引时候,使用join参数进行合并,有inner和outer方式 默认为outer,外连接,inner,内连接
print(res)
'''
outer
    a   b   c   d     e
0  0.0   1   2   3   NaN
1  4.0   5   6   7   NaN
2  8.0   9  10  11   NaN
3  NaN  36  37  38  39.0
4  NaN  40  41  42  43.0
5  NaN  44  45  46  47.0
------------------------
inner
    b   c   d
0   1   2   3
1   5   6   7
2   9  10  11
3  36  37  38
4  40  41  42
5  44  45  46

'''
# res = pd.concat([df1, df4], axis=1, join_axes = [df1.index]) #现版本pandas已弃用join_axes参数
res = df1.append([df2, df3],ignore_index=True)
print(res)
# append 增加 将df1与df2纵向连接
s1 = pd.Series([1, 2, 3, 4],index =['a', 'b', 'c', 'd'])
res = df1.append(s1,ignore_index=True)
print(res)
# 相当于在df1下面添加一行列表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值