pandas: pd.concat 用法

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pandas as pd
>>> from pandas import Series
>>> import numpy as np
>>> s1=pd.Series(np.arange(10,13))
>>> s1
0    10
1    11
2    12
dtype: int32
>>> np.arange(10,13)
array([10, 11, 12])
>>> s2=pd.Series(np.arange(100,103))
>>> s2
0    100
1    101
2    102
dtype: int32
>>> pd.concat([s1,s2])
0     10
1     11
2     12
0    100
1    101
2    102
dtype: int32
>>> pd.concat([s1,s2],axis=1)
    0    1
0  10  100
1  11  101
2  12  102
>>> pd.concat([s1,s2],keys=[1,2])
1  0     10
   1     11
   2     12
2  0    100
   1    101
   2    102
dtype: int32
>>> pd.concat([s1,s2], keys = [1,2],names = ['from','ID'])
from  ID
1     0      10
      1      11
      2      12
2     0     100
      1     101
      2     102
dtype: int32
>>> pd.concat([s1,s2], axis = 1,keys = ['s1','s2'],names = ['from','ID'])
   s1   s2
0  10  100
1  11  101
2  12  102
>>> idx = 'this is a fake data'.split()
>>> idx
['this', 'is', 'a', 'fake', 'data']
>>> df1 = pd.DataFrame({'Country':['China','Japan','Germany','USA','UK'],'Team':['A','B','A','C','D']},index = idx)
>>> df1
      Country Team
this    China    A
is      Japan    B
a     Germany    A
fake      USA    C
data       UK    D
>>> col = 'Country Team'.split()
>>> idx_2 = ['fake','world']
>>> values = [['KLR',100],['abc',200]]
>>> df2 = pd.DataFrame(values,index = idx_2, columns = col)
>>> df2
      Country  Team
fake      KLR   100
world     abc   200
>>> pd.concat([df1,df2])
       Country Team
this     China    A
is       Japan    B
a      Germany    A
fake       USA    C
data        UK    D
fake       KLR  100
world      abc  200
>>> pd.concat([df1,df2],axis=1)

Warning (from warnings module):
  File "__main__", line 1
FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

       Country  Team Country   Team
a      Germany     A     NaN    NaN
data        UK     D     NaN    NaN
fake       USA     C     KLR  100.0
is       Japan     B     NaN    NaN
this     China     A     NaN    NaN
world      NaN   NaN     abc  200.0
>>> col = ['Team','SBF']
>>> idx_3= ['true','world']
>>> values3 = [['red','pm'],['orange','pl']]
>>> df3 = pd.DataFrame(values3,index = idx_3, columns = col)
>>> df3
         Team SBF
true      red  pm
world  orange  pl
>>> df1
      Country Team
this    China    A
is      Japan    B
a     Germany    A
fake      USA    C
data       UK    D
>>> pd.concat([df1,df3])
       Country  SBF    Team
this     China  NaN       A
is       Japan  NaN       B
a      Germany  NaN       A
fake       USA  NaN       C
data        UK  NaN       D
true       NaN   pm     red
world      NaN   pl  orange
>>> df2
      Country  Team
fake      KLR   100
world     abc   200
>>> pd.concat([df2,df3])
      Country  SBF    Team
fake      KLR  NaN     100
world     abc  NaN     200
true      NaN   pm     red
world     NaN   pl  orange
>>> df3
         Team SBF
true      red  pm
world  orange  pl
>>> df1
      Country Team
this    China    A
is      Japan    B
a     Germany    A
fake      USA    C
data       UK    D
>>> df3
         Team SBF
true      red  pm
world  orange  pl
>>> df3
         Team SBF
true      red  pm
world  orange  pl
>>> df2
      Country  Team
fake      KLR   100
world     abc   200
>>> pd.concat([df2,df3])
      Country  SBF    Team
fake      KLR  NaN     100
world     abc  NaN     200
true      NaN   pm     red
world     NaN   pl  orange
>>> pd.concat([df2,df3],axis = 1)
      Country   Team    Team  SBF
fake      KLR  100.0     NaN  NaN
true      NaN    NaN     red   pm
world     abc  200.0  orange   pl
>>> pd.concat([df1.Team,df2.Team,df3.Team])
this          A
is            B
a             A
fake          C
data          D
fake        100
world       200
true        red
world    orange
Name: Team, dtype: object
>>> pd.concat(df1['Team'],df2['Team'],df3['Team'])
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    pd.concat(df1['Team'],df2['Team'],df3['Team'])
  File "C:\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 225, in concat
    copy=copy, sort=sort)
  File "C:\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 241, in __init__
    '"{name}"'.format(name=type(objs).__name__))
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"
>>> pd.concat(df1[['Team']],df2[['Team']],df3[['Team']])
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    pd.concat(df1[['Team']],df2[['Team']],df3[['Team']])
  File "C:\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 225, in concat
    copy=copy, sort=sort)
  File "C:\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 241, in __init__
    '"{name}"'.format(name=type(objs).__name__))
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
>>> pd.concat([df2,df3])
      Country  SBF    Team
fake      KLR  NaN     100
world     abc  NaN     200
true      NaN   pm     red
world     NaN   pl  orange
>>> pd.concat([df2,df3],join = 'inner')
         Team
fake      100
world     200
true      red
world  orange
>>> pd.concat([df2,df3],join = 'outer')
      Country  SBF    Team
fake      KLR  NaN     100
world     abc  NaN     200
true      NaN   pm     red
world     NaN   pl  orange
>>> df1
      Country Team
this    China    A
is      Japan    B
a     Germany    A
fake      USA    C
data       UK    D
>>> df2
      Country  Team
fake      KLR   100
world     abc   200
>>> df3
         Team SBF
true      red  pm
world  orange  pl
>>> pd.concat([df2,df3])
      Country  SBF    Team
fake      KLR  NaN     100
world     abc  NaN     200
true      NaN   pm     red
world     NaN   pl  orange
>>> pd.concat([df2,df3], ignore_index = True)
  Country  SBF    Team
0     KLR  NaN     100
1     abc  NaN     200
2     NaN   pm     red
3     NaN   pl  orange
>>> 

参考:https://blog.csdn.net/ycyrym/article/details/105127980
https://blog.csdn.net/Asher117/article/details/84799845
https://www.cnblogs.com/wzdLY/p/9673767.html
https://blog.csdn.net/Asher117/article/details/84725199
https://blog.csdn.net/stevenkwong/article/details/52528616

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值