美文网首页
python基础-12-数据分析python——pandas——

python基础-12-数据分析python——pandas——

作者: 比特跃动 | 来源:发表于2019-03-26 21:02 被阅读0次

本章内容包括: concat, join, merge


导入数据

//input1
import pandas as pd
df1 = pd.DataFrame(
    {
        'A':list('abcd'),
        'B':list('efgh'),
        'C':list('ijkl')
    }
)
df1




//output1
    A   B   C
0   a   e   i
1   b   f   j
2   c   g   k
3   d   h   l








//input2
df2 = pd.DataFrame(
    {
       #'C':list('ijkl'), #字段相同,内容相同。merge会去重;join会报错;concat在下面加;concat axis=1正常
       # 'C':list('mnop'), #字段相同,内容不同。merge会报错;join会报错;concat在下面加;concat axis=1正常
       #'C1':list('ijkl'), #字段不同,内容相同。merge报错;join正常;concat正常;concat axis=1正常
        'D':list('opqr'),
        'E':list('stuv')
    }
)
df2


//output2

    C   D   E
0   i   o   s
1   j   p   t
2   k   q   u
3   l   r   v









//input3
df1.merge(right = df2,how = 'inner')



//output3
    A   B   C   D   E
0   a   e   i   o   s
1   b   f   j   p   t
2   c   g   k   q   u
3   d   h   l   r   v








//input4
df1.join(df2)



//output4
    A   B   C   C1  D   E
0   a   e   i   i   o   s
1   b   f   j   j   p   t
2   c   g   k   k   q   u
3   d   h   l   l   r   v








//input6
pd.concat([df1,df2])



//output6
    A   B   C   C1  D   E
0   a   e   i   NaN NaN NaN
1   b   f   j   NaN NaN NaN
2   c   g   k   NaN NaN NaN
3   d   h   l   NaN NaN NaN
0   NaN NaN NaN i   o   s
1   NaN NaN NaN j   p   t
2   NaN NaN NaN k   q   u
3   NaN NaN NaN l   r   v








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



//output7

    A   B   C   C   D   E
0   a   e   i   i   o   s
1   b   f   j   j   p   t
2   c   g   k   k   q   u
3   d   h   l   l   r   v

相关文章

网友评论

      本文标题:python基础-12-数据分析python——pandas——

      本文链接:https://www.haomeiwen.com/subject/dlnnvqtx.html