美文网首页
合并dataframe

合并dataframe

作者: Kevin不会创作 | 来源:发表于2022-07-06 15:21 被阅读0次

    Pandas

    • 连接两个dataframe
      1. pd.merge()
    pd.merge(left, right, how='left', on='key')
    
    merge
    1. pd.join()

    join method combines two dataframes on the basis of their indexes.
    merge method is more versatile and allows us to specify columns beside the index to join on for both dataframes.

    1. df.append()
    result=df1.append(df2)
    
    append

    如果两张表的表结构不一致,则返回的column中可能会出现NaN.

    df1 = pd.DataFrame({"a":[1, 2, 3, 4],
                        "b":[5, 6, 7, 8]})
      
    df2 = pd.DataFrame({"a":[1, 2, 3],
                        "b":[5, 6, 7], 
                        "c":[1, 5, 4]})
      
    df1.append(df2, ignore_index = True)
    
    append with NaN
    1. df.concat()
      frames = [df1, df2, df3]
      result = pd.concat(frames)
    
    concate

    concat gives the flexibility to join based on the axis(all rows or all columns).
    append is the specific case(axis=0, join='outer') of concat.

    相关文章

      网友评论

          本文标题:合并dataframe

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