- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道这个话题已经被讨论了一千次了。但我想不出解决办法。
我正在尝试计算列表(df1.list1 的每一行)在列表 (df2.list2) 的列中出现的频率。所有列表仅包含唯一值。 List1 包括大约 300.000 行和 list2 30.000 行。
我有一个可以工作的代码,但速度非常慢(因为我使用的是 iterrows)。我也试过 itertuples() 但它给了我一个错误(“解压的值太多(预期 2)”)。我在网上发现了一个类似的问题:Pandas counting occurrence of list contained in column of lists .在提到的情况下,该人仅考虑在一列列表中出现一个列表。但是,我无法解决问题,因此将 df1.list1 中的每一行与 df2.list2 进行比较。
这就是我的列表的样子(简化):
df1.list1
0 ["a", "b"]
1 ["a", "c"]
2 ["a", "d"]
3 ["b", "c"]
4 ["b", "d"]
5 ["c", "d"]
df2.list2
0 ["a", "b" ,"c", "d"]
1 ["a", "b"]
2 ["b", "c"]
3 ["c", "d"]
4 ["b", "c"]
我想要的是:
df1
list1 occurence
0 ["a", "b"] 2
1 ["a", "c"] 1
2 ["a", "d"] 1
3 ["b", "c"] 3
4 ["b", "d"] 1
5 ["c", "d"] 2
这就是我到目前为止所得到的:
for index, row in df_combinations.iterrows():
df1.at[index, "occurrence"] = df2["list2"].apply(lambda x: all(i in x for i in row['list1'])).sum()
有什么可以加快速度的建议吗?提前致谢!
最佳答案
这应该快得多:
df = pd.DataFrame({'list1': [["a","b"],
["a","c"],
["a","d"],
["b","c"],
["b","d"],
["c","d"]]*100})
df2 = pd.DataFrame({'list2': [["a","b","c","d"],
["a","b"],
["b","c"],
["c","d"],
["b","c"]]*100})
list2 = df2['list2'].map(set).tolist()
df['occurance'] = df['list1'].apply(set).apply(lambda x: len([i for i in list2 if x.issubset(i)]))
使用您的方法:
%timeit for index, row in df.iterrows(): df.at[index, "occurrence"] = df2["list2"].apply(lambda x: all(i in x for i in row['list1'])).sum()
1 loop, best of 3: 3.98 s per loop Using mine:
%timeit list2 = df2['list2'].map(set).tolist();df['occurance'] = df['list1'].apply(set).apply(lambda x: len([i for i in list2 if x.issubset(i)]))
10 loops, best of 3: 29.7 ms per loop
请注意,我将列表的大小增加了 100 倍。
编辑
这个看起来更快:
list2 = df2['list2'].sort_values().tolist()
df['occurance'] = df['list1'].apply(lambda x: len(list(next(iter(())) if not all(i in list2 for i in x) else i for i in x)))
和时间:
%timeit list2 = df2['list2'].sort_values().tolist();df['occurance'] = df['list1'].apply(lambda x: len(list(next(iter(())) if not all(i in list2 for i in x) else i for i in x)))
100 loops, best of 3: 14.8 ms per loop
关于python - 更快地替代 iterrows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149735/
我编写了以下代码来创建数据框,并根据特定条件添加新的行和列。不幸的是,执行起来需要很多时间。 有其他方法可以做到这一点吗?任何意见都将受到高度赞赏。 dfCircuito=None for index
我有一个pandas数据框 from pandas import DataFrame, Series 其中每一行对应一个案例,每一列对应一个月份。我想在每 12 个月期间执行一次滚动求和。看起来很简单
我知道这个话题已经被讨论了一千次了。但我想不出解决办法。 我正在尝试计算列表(df1.list1 的每一行)在列表 (df2.list2) 的列中出现的频率。所有列表仅包含唯一值。 List1 包括大
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我需要从 6 列数据帧的单行中选择 3 列,并将其附加到另一个只有 3 列的数据帧。 import pandas import numpy df1 = pd.DataFrame({'Name':['S
这个问题已经有答案了: Create Pandas DataFrame from txt file with specific pattern (6 个回答) 已关闭 4 年前。 我正在研究代表美国地
以下是示例数据 - 当前解决方案 - for idx, row in data.iterrows(): rp = row['right_player_test'] lp = row['
当我为每个客户创建新的数据框架时,我还想创建一个将所有这些数据框架附加在一起的巨大数据框架。 我创建了一个函数来按照我需要的方式对用户数据进行分组。现在我想迭代另一个包含唯一用户 key 的数据帧,并
d = {'2014-07-10 08:26:29.091199': {'Col':'12976482066142964'}, '2014-07-10 08:26:29.171198': {'Col'
我有一个数据集,其中包含几年来每天的美国国债曲线。行 = 日期,列 = 特定国债期限(3 个月、1 年、10 年等) 我有每天循环并校准利率模型参数的Python代码。我在通过 iterrows 和循
我有一个 pandas Dataframe 的形式: A B K S 2012-03-31 NaN NaN
我讨论了 iterrows 的性能问题 previously , 并得到了良好的普遍 react 。这个问题是一个特定的案例,我希望你能帮助我更好地应用一些东西,因为 iterrows 很慢。 我相信
我在 pandas 中有一个 df,它看起来像: id name values 1 a cat dog 2 b bird fly 我目前在做: for index, row
我相信有人也在 this thread 中报告过, 使用 iterrows() 填充数据框可能导致持久性问题。例如。像这样简单的东西: my_dataframe = pd.DataFrame(np.N
以下代码读取 csv 文件并遍历“子”列的行: df = pd.read_csv("Subs_test.csv", usecols= ['sub']) for index, row in df.ite
我是编程新手,到目前为止,这个问题已经“超出了我的薪水等级”,因此非常感谢您对此提供的任何帮助! 我有一个如下所示的 DataFrame 对象: Dan
我有两个数据集。 df Name Date Quantity ZMTD 2018-06-30 1000 ZMTD 2018-05-31 975 ZMT
我想知道在使用 iterrows 遍历 DataFrame 时如何使用索引而不是名称访问列。 这段代码是我能找到的最多的: for index, row in df.iterrows(): p
我有两个非常相似的 iterrows 循环,一个按我的预期工作,另一个不工作。我的问题是什么使第二个与第一个不同,为什么这会改变我的循环的行为?谢谢 工作循环 我正在使用数据框 drugs_tall
我是一名优秀的程序员,十分优秀!