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