python中是否有for循环的替代方法,无法过滤我的数据

时间:2019-08-03 15:16:21

标签: python

我有两个数据框,是使用pandas从excel导入的。这些可能包含一些记录 inv有

GTIN           Invoice_number
00047997123657 123
00076606101311 124
88888888888888 125

而gtin有

GTIN            Department_code
00047997123657  101
00076606101311  102
04008100150157  103

我尝试过

for i, row in inv.iterrows():
for j, rows in gtin.iterrows():
    if row['GTIN'] == rows['GTIN']:
        invnum = (row['Invoice'])
        depnum = (rows['Department'])
        arrays.append([invnum, depnum])
result = pd.DataFrame(arrays,columns=['Invoice Number','Department Code'])
print(result)

如果我愿意

for i, row in inv.iterrows():
for j, rows in gtin.iterrows():
    if row['GTIN'] == rows['GTIN']:
        invnum = (row['Invoice'])
        depnum = (rows['Department'])
        arrays.append([invnum, depnum])
    elif row['GTIN'] != rows['GTIN']:
        invnum = (row['Invoice'])
        depnum = "GTIN not Found"
        arrays.append([invnum, depnum])

我得到

  Invoice Number  Department Code
0            123              101
1            124              102         

Invoice Number,Department Code
0             123             101
1             123  GTIN not Found
2             123  GTIN not Found
3             124  GTIN not Found
4             124             102
5             124  GTIN not Found
6             125  GTIN not Found
7             125  GTIN not Found
8             125  GTIN not Found

分别,我如何获得

Invoice Number  Department Code
0             123              101
1             124              102
2             125              GTIN not Found

是否有其他方法可以像python中的linq一样过滤此数据?

0 个答案:

没有答案