[英]Pandas conditionally merge dataframes together
我有以下2個數據框:
df1:
col1 col2 col3 col4
0 31 53 82
1 23 73 32
2 35 34 12
3 36 13 24
4 23 93 36
df2:
col1 col5 col6 col7
2 315 324 122
3 316 123 224
4 213 923 326
我想合並兩個數據框,但想刪除一些我不需要的列。 盡管應刪除索引均不在兩個df中的行。
df3:
col1 col2 col4 col5 col7
2 35 12 315 122
3 36 24 316 224
4 23 36 213 326
我怎樣才能做到這一點? 謝謝!
刪除不需要的列並在col1
合並。
df = df1.drop(['col3'], 1).merge(df2.drop(['col6'], 1), on='col1')
df
col1 col2 col4 col5 col7
0 2 35 12 315 122
1 3 36 24 316 224
2 4 23 36 213 326
您可以根據需要添加更多列以放入列表參數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.