python - 如何使用 pandas 将数字列表与 Excel 文档中的列进行比较?

标签 python excel pandas

我有这个简单的脚本,它通过从 excel_1 的列中取出值并将其与 excel_2 中的列的值进行比较来创建一个列表。我希望它返回不在 excel_2 中的“ext_addr_id”列中的任何行。
如果我对列表进行硬编码,它会很好地创建文档。但是当我使用 for 循环创建列表时,它会返回整个列表并且似乎不进行比较,或者至少在进行比较时,它不会将列表中的数字识别为与数字相同在 excel_2 中。
我已经尝试了一切,但我没有想法,我错过了什么?

import pandas as pd
import os

filePath = r'/outputs/coupa_address_id_translation_results.xlsx'
if os.path.exists(filePath):
    os.remove(filePath)

cust_results = pd.read_excel(r'./data_sources/excel_1.xlsx')
# maybe convert to csv to get access to other libs
# aws_results = pd.read_excel(r'./data_sources/excel_2.xlsx')
aws_results = pd.read_csv(r'./data_sources/excel_2.csv')

missing_addresses = pd.DataFrame()
cust_addr_id_list = []

for cust_row, cust_col in cust_results.iterrows():
    cust_ext_addr_id_column_number = 0  # index begins at 0
    cust_addr_id_list.append(str(cust_results.values[cust_row][cust_ext_addr_id_column_number]).strip())
    # cust_addr_id_list.append(cust_results.values[cust_row][cust_ext_addr_id_column_number])
    
print(cust_addr_id_list)
filter = ~aws_results['ext_addr_id'].isin(cust_addr_id_list)
# filter = ~aws_results['ext_addr_id'].isin(['1253', '2177', '2258'])

# print(filter)

missing_addresses = aws_results[filter]

missing_addresses.to_excel("./outputs/coupa_address_id_translation_results.xlsx")
感谢您的任何帮助,您可以提供
编辑:响应 Durtal re:cust_addr_id_list 的内容,我已经
用字符串和整数都试过了,实际上这是我问题的症结所在。输出是['2339', '2534', '2234', '2340', '2363', '2297', '2834', '2371', '2190', '2373', '2284', '2287', '1821', '1898', '2342', '2374', '2311', '2343', '2344', '2376', '2193', '2192', '2345', '2346', '2191', '2379', '2383', '2293', '1887', '2513', '675', '1888', '2235', '2269', '2270', '2272', '2224', '2223', '1769', '1897', '1771', '2286', '2537', '1899', '2382', '2225', '2283', '2276', '2285', '683', '2161', '2271', '2174', '2173', '1781', '2335', '2347', '2317', '1780', '2521', '1906', '2349', '2328', '2348', '2327', '1782', '2207', '2210', '2840', '2208', '2260', '2184', '2321', '2183', '2182', '2322', '2333', '2350', '2692', '2717', '2690', '2720', '2718', '2719', '2310', '2266', '2313', '1893', '2265', '2314', '2337', '2308', '2309', '2315', '2351', '2352', '1541', '1890', '2298', '1889', '1891', '2240', '2242', '2370', '2217', '2244', '2501', '2301', '2331', '2319', '2238', '1911', '2329', '2689', '2728', '2729', '2688', '2727', '2730', '2731', '2723', '2290', '2687', '2724', '2725', '2726', '2330', '2166', '2168', '2167', '2257', '2169', '1910', '2282', '2209', '2336', '2517', '2171', '2362', '2307', '1883', '2197', '2299', '2320', '1913', '2303', '2318', '2316', '2858', '1903', '1901', '1902', '2300', '1905', '2304', '2326', '2302', '2250', '2393', '2355', '2356', '2366', '2252', '2357', '1894', '2375', '2843', '1909', '2359', '2377', '1895', '2433', '2196', '1907', '1738', '1786', '2237', '2175', '2261', '1784', '2365', '2236', '2259', '2323', '1846', '2443', '2248', '2369', '2204', '2253', '2216', '2212', '2457', '2214', '2213', '1790', '2258', '1920', '2215', '2177', '2178', '2188', '2189', '2636', '2754', '2202', '2263', '2427', '2222', '2262', '2220', '2281', '1900', '2305', '1904', '1908', '2221', '2438', '2360', '2425', '2334', '2761', '2295', '2312', '2361', '2288', '2650', '2543', '2251', '2860', '2735', '2368', '2338', '2228', '2227', '2258'][2339, 2534, 2234, 2340, 2363, 2297, 2834, 2371, 2190, 2373, 2284, 2287, 1821, 1898, 2342, 2374, 2311, 2343, 2344, 2376, 2193, 2192, 2345, 2346, 2191, 2379, 2383, 2293, 1887, 2513, 675, 1888, 2235, 2269, 2270, 2272, 2224, 2223, 1769, 1897, 1771, 2286, 2537, 1899, 2382, 2225, 2283, 2276, 2285, 683, 2161, 2271, 2174, 2173, 1781, 2335, 2347, 2317, 1780, 2521, 1906, 2349, 2328, 2348, 2327, 1782, 2207, 2210, 2840, 2208, 2260, 2184, 2321, 2183, 2182, 2322, 2333, 2350, 2692, 2717, 2690, 2720, 2718, 2719, 2310, 2266, 2313, 1893, 2265, 2314, 2337, 2308, 2309, 2315, 2351, 2352, 1541, 1890, 2298, 1889, 1891, 2240, 2242, 2370, 2217, 2244, 2501, 2301, 2331, 2319, 2238, 1911, 2329, 2689, 2728, 2729, 2688, 2727, 2730, 2731, 2723, 2290, 2687, 2724, 2725, 2726, 2330, 2166, 2168, 2167, 2257, 2169, 1910, 2282, 2209, 2336, 2517, 2171, 2362, 2307, 1883, 2197, 2299, 2320, 1913, 2303, 2318, 2316, 2858, 1903, 1901, 1902, 2300, 1905, 2304, 2326, 2302, 2250, 2393, 2355, 2356, 2366, 2252, 2357, 1894, 2375, 2843, 1909, 2359, 2377, 1895, 2433, 2196, 1907, 1738, 1786, 2237, 2175, 2261, 1784, 2365, 2236, 2259, 2323, 1846, 2443, 2248, 2369, 2204, 2253, 2216, 2212, 2457, 2214, 2213, 1790, 2258, 1920, 2215, 2177, 2178, 2188, 2189, 2636, 2754, 2202, 2263, 2427, 2222, 2262, 2220, 2281, 1900, 2305, 1904, 1908, 2221, 2438, 2360, 2425, 2334, 2761, 2295, 2312, 2361, 2288, 2650, 2543, 2251, 2860, 2735, 2368, 2338, 2228, 2227, 2258]取决于我是否包装 (cust_results.values[cust_row][cust_ext_addr_id_column_number]是否在 str() 中(例如在示例中)。
在这两种情况下都不起作用。但是,如果我将出现在两个列表中的一些条目直接传递到过滤器变量中,例如 # filter = 行注释掉,然后脚本工作并返回正确的行。

最佳答案

您的代码有几个问题,但这次我们将坚持您的逻辑。
为变量使用内置函数的名称是个坏主意。使用filter_而不是 filter ,或者更好:给它一个合适的名字,比如 index_not_matched .
自助食谱:
如果返回整个数据帧,这可能意味着您的过滤器变量仅包含 True .检查这一点,然后检查为什么会发生这种情况!
显然 filter = ~aws_results['ext_addr_id'].isin(cust_addr_id_list)没有按预期工作。检查为什么!
两个列表的项目是否具有相同的数据类型?看aws_results.dtypes[type(el) for el in cust_addr_id_list] .
可能的类型有:字符串、整数、浮点数。确保两个列表属于同一类型。然后确保两个列表包含相同的值。这应该将您带到您想要的地方。

关于python - 如何使用 pandas 将数字列表与 Excel 文档中的列进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71930451/

相关文章:

excel - 在 Excel 中动态更新日期

python - 用列/系列中的值替换 pandas 子字符串

python - 如果 Python 中的条件为真,则跳过一段代码

python - 是否有用于调试 python 的 sublime text 2 插件?

android - 如何在Python中区分 "Android"和其他 "Linux"操作系统?

python - DataFrame 'groupby' 正在修复带有索引的组列

python - 如何更新数据帧值

python - 嵌套字典和检索键/值

c# - 使用名称访问工作表上的 CustomProperties

c# - 在 Azure 上使用 Excel