为使您更容易理解我的问题,我将在此之前进行总结。
“今天,我只查询以前从未访问过的平面文件,但是相同的代码可用于同一MongoDB集合中的其他平面文件。”
在细节下方,
我有一个查询customer_id
的列表,我的列表称为alist
[7068, 7116, 7154, 7342, 7379]
我正在在Python上使用pandas
和pymongo
进行MongoDB查询。这是我的MongoDB查询,这是我导入的库
import pandas as pd
from pymongo import MongoClient
import datetime as dt
此后,我提供了数据库凭据。这是凭证
mongo_client = MongoClient(host= ... ,port= ... ,username= ...,password= ... ,authSource='admin')
db = mongo_client['something-info']
cv = db['flat_something']
这是查询
data = cv.find()
query_filter_alist = {'customer_id': {'$in': alist}}
query_project = {'_id':0}
cursor_list = cv.find(query_filter_alist, query_project)
contacts = pd.DataFrame(list(cursor_list)).drop_duplicates()
它可用于同一MongoDB集合上的其他flat_file,但不适用于此平面文件。这是错误消息
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<timed exec> in <module>()
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in drop_duplicates(self, subset, keep, inplace)
3096 """
3097 inplace = validate_bool_kwarg(inplace, 'inplace')
-> 3098 duplicated = self.duplicated(subset, keep=keep)
3099
3100 if inplace:
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in duplicated(self, subset, keep)
3142
3143 vals = (self[col].values for col in subset)
-> 3144 labels, shape = map(list, zip(*map(f, vals)))
3145
3146 ids = get_group_index(labels, shape, sort=False, xnull=False)
~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in f(vals)
3131 def f(vals):
3132 labels, shape = algorithms.factorize(
-> 3133 vals, size_hint=min(len(self), _SIZE_HINT_LIMIT))
3134 return labels.astype('i8', copy=False), len(shape)
3135
~/anaconda3/lib/python3.6/site-packages/pandas/core/algorithms.py in factorize(values, sort, order, na_sentinel, size_hint)
558 uniques = vec_klass()
559 check_nulls = not is_integer_dtype(original)
--> 560 labels = table.get_labels(values, uniques, 0, na_sentinel, check_nulls)
561
562 labels = _ensure_platform_int(labels)
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_labels()
TypeError: unhashable type: 'list'
我猜问题出在flat_something
文件上,但是我想我需要做几次检查才能了解确切的问题。任何建议都会很有帮助
答案 0 :(得分:1)
提供一个cursor_list
的示例,以及在没有错误或没有contacts
的情况下drop_duplicates()
的外观。使用此示例,当传入的值之一是列表['a', 'b']
时,会出现错误:
In [2]: pd.DataFrame(pd.Series([['a', 'b'], 'c', ['a', 'b']])) # ok
Out[2]:
0
0 [a, b]
1 c
2 [a, b]
In [3]: pd.DataFrame(pd.Series([['a', 'b'], 'c', ['a', 'b']])).drop_duplicates() # error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
所有列表值都需要转换为hashable,这是Python用来确定值对于删除重复项唯一的方法。如元组:
In [5]: df = pd.DataFrame(pd.Series([['a', 'b'], 'c', ['a', 'b']])) # duplicate
In [6]: df.apply(lambda x: tuple(*x), axis=1)
Out[6]:
0 (a, b)
1 (c,)
2 (a, b)
dtype: object
In [7]: df.apply(lambda x: tuple(*x), axis=1).drop_duplicates()
Out[7]:
0 (a, b)
1 (c,)
dtype: object
您可能需要分两个步骤进行操作:首先加载,然后应用+拖放:
contacts = pd.DataFrame(list(cursor_list))
contacts = contacts.apply(lambda x: tuple(*x), axis=1).drop_duplicates()
并确保在需要它的特定列上使用它,而不是全部。