我有两个数据帧:
[in] print(testing_df.head(n=5))
print(product_combos1.head(n=5))
[out]
product_id length
transaction_id
001 (P01,) 1
002 (P01, P02) 2
003 (P01, P02, P09) 3
004 (P01, P03) 2
005 (P01, P03, P05) 3
product_id count length
0 (P06, P09) 36340 2
1 (P01, P05, P06, P09) 10085 4
2 (P01, P06) 36337 2
3 (P01, P09) 49897 2
4 (P02, P09) 11573 2
我想返回频率最高的product_combos
行,它们是len(testing_df + 1)
,并且包含testing_df
字符串。例如,事务id 001我想返回product_combos[3]
(尽管只有P09)
对于第一部分(仅根据长度进行比较),我尝试:
# Return the product combos values that are of the appropriate length and the strings match
for i in testing_df['length']:
for k in product_combos1['length']:
if (i)+1 == (k):
matches = list(k)
但是,这将返回错误:
TypeError: 'numpy.int64' object is not iterable
只需使用.append()方法。我还建议将“matches”设置为顶部的空列表,这样在重新运行单元格时就不会出现重复
让我知道这是否有效,或者如果有什么其他的!祝你好运
你不能从那样的不可iterable创建列表。尝试用
matches = [k]
替换matches = list(k)
。 另外,这些括号是多余的-您可以用if i + 1 == k:
替换if (i)+1 == (k):
相关问题 更多 >
编程相关推荐