- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在构建一个模糊搜索程序,使用 FuzzyWuzzy 在数据集中查找匹配的名称。正如预期的那样,我的数据位于大约 10378 行的 DataFrame 中,len(df['Full name'])
是 10378。但是 len(choices)
只有 1695。
我在 IPython Notebook 中运行 Python 2.7.10
和 pandas 0.17.0
。
choices = df['Full name'].astype(str).to_dict()
def fuzzy_search_to_df (term, choices=choices):
search = process.extract(term, choices, limit=len(choices)) # does the search itself
rslts = pd.DataFrame(data=search, index=None, columns=['name', 'rel', 'df_ind']) # puts the results in DataFrame form
return rslts
results = fuzzy_search_to_df(term='Ben Franklin') # returns the search result for the given term
matches = results[results.rel > 85] # subset of results, these are the best search results
find = df.iloc[matches['df_ind']] # matches in the main df
正如您可能知道的那样,我在 choices
字典中获取结果的索引作为 df_ind
,我原以为它与索引相同在主数据框中。
我相当确定问题出在第一行,to_dict()
函数,如 len(df['Full name'].astype(str)
结果为 10378,len(df['Full name'].to_dict())
结果为 1695。
最佳答案
问题是您的数据框中有多行,其中索引相同,因此由于 Python 字典只能为单个键保存单个值,并且在 Series.to_dict()
中方法中,索引用作键,这些行中的值将被后面的值覆盖。
展示这种行为的一个非常简单的例子-
In [36]: df = pd.DataFrame([[1],[2]],index=[1,1],columns=['A'])
In [37]: df
Out[37]:
A
1 1
1 2
In [38]: df['A'].to_dict()
Out[38]: {1: 2}
这就是您的情况,并从评论中注意到,因为索引的 unique
值的数量仅为 1695
,我们可以通过以下方式确认这一点测试 len(df.index.unique())
的值。
如果您满足于将数字作为 key
(数据框的索引),那么您可以使用 DataFrame.reset_index()
重置索引,然后在上面使用 .to_dict()
。示例 -
choices = df.reset_index()['Full name'].astype(str).to_dict()
上面示例的演示 -
In [40]: df.reset_index()['A'].to_dict()
Out[40]: {0: 1, 1: 2}
这与 OP 找到的解决方案相同 - choices = dict(zip(df['n'],df['Full name'].astype(str)))
(可以是从评论中看到)——但这种方法比使用 zip
和 dict
更快。
关于python - to_dict 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338853/
如何保留 header 名称的顺序。该顺序不是按字母顺序排列的,而是自定义顺序。在数据框中没问题,但是当我执行 to_dict 时,顺序不是我原来的顺序。 这是我在类里面使用的方法。 #the li
我正在尝试从我的 pandas DataFrame 创建默认字典,但 to_dict() 方法在我要写入的列的值周围创建了不需要的方括号。示例代码如下: # Create DF my_df = pd.
我正在构建一个模糊搜索程序,使用 FuzzyWuzzy 在数据集中查找匹配的名称。正如预期的那样,我的数据位于大约 10378 行的 DataFrame 中,len(df['Full name'])
在我的 Pandas DataFrame 中,我有一些日期值,我使用 datetime 从时间戳转换为日期时间。模块。打印出 DataFrame 看起来不错,但是当我使用 to_dict() 将 Da
我正在尝试将 pandas DataFrame 转换为字典列表,其中 1 个字典代表 1 行;因此 pandas to_dict(orient='records') 方法是完美的;然而,在某些情况下,
我正在尝试创建字典,但无法按我预期的方式运行。我觉得我很亲近。我是 yelp 数据的 df: import pandas as pd file_rev = 'blah.csv' reviews=pd.
我有一个 DataFrame,它似乎以下列方式出现异常(?): >>> a=z.to_dict(orient='records') >>> b=z.to_dict(orient='records')
我的主要观点是: assert_frame_equal(DataFrame.from_dict(df.to_dict()), df) 在某些情况下会失败。我很乐意提供一个可重现的示例,但是(i)数据太
我正在利用 ndb 的 to_dict 方法将对象的属性转换为 python 字典。据我所知,根据文档,此方法不包括字典中对象的键或父级: https://developers.google.com/
我有一个 pandas DataFrame df包含 Timesatamp列。 我希望从 iter.. 创建一个行迭代器(通过 to_dict 方法或通过 df )哪里Timesatamp值是 pyt
我有以下数据框df: \def \orth
我有以下数据框: 注意:日期是索引 city morning afternoon evening midnight date 2014-05-01 Y
我正在尝试将我的数据框转换为字典,以便使用这些字典来实例化一些类对象。遵循文档,http://pandas.pydata.org/pandas-docs/stable/reference/api/pa
这篇文章的要点是我的原始数据中有“23”,我希望在我的结果字典中有“23”(而不是“23.0”)。以下是我尝试使用 Pandas 处理它的方法。 我的 Excel 工作表有一个编码区域列: 23 11
嗯,这很尴尬......我正在尝试创建一个 good reproducible pandas example通过给你们我的数据集的一个小样本。我认为使用 df.to_dict() 会很简单,但无济于事
我下面的代码接收 CSV 数据,并使用 pandas to_dict() 函数作为将数据转换为 JSON 的一个步骤。 问题是它正在修改 float (例如 1.6 变成 1.600000000000
如果您有一个带有唯一索引的简洁 pandas Series 对象,那么使用 pd.Series.to_dict() 可以如您所愿:它变成一个 Python dict,每个索引都指向其各自的值。 如果您
Pandas 有一个非常好的功能,可以通过 pd.to_dict('records') 将我们的数据帧导出到字典列表中。 . 例如: d = pd.DataFrame({'a':[1,2,3], 'b
尽管在 How do I ask a good question? 上有明确的指导和 How to create a Minimal, Reproducible Example ,许多人似乎只是忽略了
我使用返回大型 pandas 数据帧的 api。我不知道直接迭代数据帧的快速方法,因此我使用 to_dict() 转换为字典。 我的数据转成字典形式后,性能还不错。然而,to_dict() 操作往往是
我是一名优秀的程序员,十分优秀!