合并dataframe

2022-07-06  本文已影响0人  Kevin不会创作

Pandas

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.

上一篇下一篇

猜你喜欢

热点阅读