【发布时间】:2021-04-18 18:45:00
【问题描述】:
我有两个数据框,都有一列“ITEM” 我正在尝试实现这一点,进行适当的合并,因此结果只是左侧数据帧中的值,与第二个匹配,其余部分除外。我阅读并尝试了几种解决方案,并认为左合并可以解决问题,但它不起作用
import pandas as pd
# filenames = ['newsc.csv','oldsc.csv']
# newsc has the label ITEM
''' values
400012
400016
400021
400028
400038
400042
400050
400056
400084
400088
400105
400148
400154
400173
400181
400184
400193
400197
400202
400209
423316
429247
441835
446011
446012
447164
454086
457076
459524
467432
503013
514979
524815
535084
541730
545113
547630
548622
'''
# oldsc also uses the same label 'ITEM'
''' values
400015
400016
400019
400021
400028
400033
400037
400038
400042
400050
400056
400057
400061
400063
400068
400069
400073
400077
400084
400091
400092
400097
400102
400105
400113
400118
400121
400122
400126
400127
400128
400132
400133
400134
400141
400148
400153
400154
'''
# I'm reading the above values from both csv files to compare what
# values in df1 are present in df2
df = pd.read_csv('newsc.csv')
df1 = pd.read_csv('oldsc.csv')
df3 = pd.merge(df, df1, left_on='ITEM', right_on='ITEM', how ='left')
print(df3.head(13))
我期待得到这些
400016
400021
400028
400038
400042
400050
400056
400084
400105
400148
400154
相反,我得到了这些结果
ITEM
0 400012
1 400016
2 400021
3 400028
4 400038
5 400042
6 400050
7 400056
8 400084
9 400088
10 400105
11 400148
12 400154
【问题讨论】: