python concat_Pandas的数据拼接-concat函数

25. Pandas的数据拼接-concat函数

在pandas里提供concat函数可以将形参给出的列表里的各个pandas的数据拼接成一个大的数据。

两个Series的拼接

import pandas as pd

import numpy as np

s1 = pd.Series(np.arange(2,6))

s2 = pd.Series(np.arange(8,12))

ss = pd.concat([s1, s2])

print ss

程序的执行结果:

0 2

1 3

2 4

3 5

0 8

1 9

2 10

3 11

dtype: int64

两个DataFrame的拼接

1). label和columns均相同的情况下:

import pandas as pd

import numpy as np

col = "hello the cruel world".split()

idx = ["a", "b", "c", "d"]

val1 = np.arange(16).reshape(4, 4)

val2 = np.arange(20, 36).reshape(4, 4)

df1 = pd.DataFrame(val1, index = idx, columns = col)

print df1

df2 = pd.DataFrame(val2, index = idx, columns = col)

print df2

df12 = pd.concat([df1, df2])

print df12

程序的执行结果:

hello the cruel world # prinf df1

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

hello the cruel world # print df2

a 20 21 22 23

b 24 25 26 27

c 28 29 30 31

d 32 33 34 35

hello the cruel world # print df12

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

a 20 21 22 23

b 24 25 26 27

c 28 29 30 31

d 32 33 34 35

2). 对于DataFrame的拼接比较复杂,原因是label和columns有可能不是一一对应的,这个时候两DataFrame未匹配上的label或columns下的值为NaN。

import pandas as pd

import numpy as np

col1 = "hello the cruel world".split()

col2 = "hello the nice world".split()

idx1 = ["a", "b", "c", "d"]

idx2 = ["a", "b", "d", "e"]

val1 = np.arange(16).reshape(4, 4)

val2 = np.arange(20, 36).reshape(4, 4)

df1 = pd.DataFrame(val1, index = idx1, columns = col1)

print df1

df2 = pd.DataFrame(val2, index = idx2, columns = col2)

print df2

df12 = pd.concat([df1, df2])

print df12

程序执行结果:

hello the cruel world # print df1

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

hello the nice world # print df2

a 20 21 22 23

b 24 25 26 27

d 28 29 30 31

e 32 33 34 35

cruel hello nice the world # print df12

a 2 0 NaN 1 3

b 6 4 NaN 5 7

c 10 8 NaN 9 11

d 14 12 NaN 13 15

a NaN 20 22 21 23

b NaN 24 26 25 27

d NaN 28 30 29 31

e NaN 32 34 33 35

指定拼接的轴,默认是列方向的拼接数据,可以指定concat 的形参axis为行上的拼接数据。

import pandas as pd

import numpy as np

col1 = "hello the cruel world".split()

col2 = "hello the nice world".split()

idx1 = ["a", "b", "c", "d"]

idx2 = ["a", "b", "d", "e"]

val1 = np.arange(16).reshape(4, 4)

val2 = np.arange(20, 36).reshape(4, 4)

df1 = pd.DataFrame(val1, index = idx1, columns = col1)

print df1

df2 = pd.DataFrame(val2, index = idx2, columns = col2)

print df2

df12 = pd.concat([df1, df2], axis = 1)

print df12

程序的执行结果:

hello the cruel world # print df1

a 0 1 2 3

b 4 5 6 7

c 8 9 10 11

d 12 13 14 15

hello the nice world# print df2

a 20 21 22 23

b 24 25 26 27

d 28 29 30 31

e 32 33 34 35

hello the cruel world hello the nice world # print df12

a 0 1 2 3 20 21 22 23

b 4 5 6 7 24 25 26 27

c 8 9 10 11 NaN NaN NaN NaN

d 12 13 14 15 28 29 30 31

e NaN NaN NaN NaN 32 33 34 35

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pd.concat()是pandas库中的一个函数,用于沿着指定的轴将多个DataFrame或Series对象拼接在一起。这个函数可以实现上下拼接,并且可以根据指定的轴进行拼接。 具体来说,可以将多个DataFrame按照列或行的方式进行拼接。通过设置axis参数,可以指定拼接的轴是列还是行。例如,当axis=0时,表示按行拼接,当axis=1时,表示按列拼接拼接的对象可以是DataFrame,也可以是Series。在拼接时,会根据索引进行对齐。如果两个对象的索引不完全对齐,那么缺失的部分将以NaN填充。如果想要丢弃掉缺失的部分,可以通过设置join参数为'inner'来实现。 总之,pd.concat()函数pandas库中用于拼接多个DataFrame或Series对象的函数,可以通过设置axis参数来指定拼接的方式,通过对齐索引来完成拼接操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [pandas的连接函数concat()函数](https://blog.csdn.net/zzpdbk/article/details/79232661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【PythonPandas宝藏函数-concat()](https://blog.csdn.net/fengdu78/article/details/122711155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值