Pandas
- 连接两个dataframe
- pd.merge()
pd.merge(left, right, how='left', on='key')
![](https://img.haomeiwen.com/i14156194/1fcf252f494963ae.png)
- 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.
- df.append()
result=df1.append(df2)
![](https://img.haomeiwen.com/i14156194/302ca8a15a7d0cfd.png)
如果两张表的表结构不一致,则返回的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)
![](https://img.haomeiwen.com/i14156194/4368489a26b221be.png)
- df.concat()
frames = [df1, df2, df3]
result = pd.concat(frames)
![](https://img.haomeiwen.com/i14156194/9930115e3e52ccce.png)
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.
网友评论