我创建了一个数据帧:
[in] testing_df =pd.DataFrame(test_array,columns=['transaction_id','product_id'])
# Split the product_id's for the testing data
testing_df.set_index(['transaction_id'],inplace=True)
testing_df['product_id'] = testing_df['product_id'].apply(lambda row: row.split(','))
[out] product_id
transaction_id
001 [P01]
002 [P01, P02]
003 [P01, P02, P09]
004 [P01, P03]
005 [P01, P03, P05]
006 [P01, P03, P07]
007 [P01, P03, P08]
008 [P01, P04]
009 [P01, P04, P05]
010 [P01, P04, P08]
现在如何从结果中删除“P04”和“P08”?在
我试过了:
^{pr2}$然而,这两种选择似乎都行不通。在
数据类型包括:
[in] print(testing_df.dtypes)
[out] product_id object
dtype: object
[in] print(testing_df['product_id'].dtypes)
[out] object
我会在分裂之前做:
数据:
解决方案:
^{pr2}$或者您可以更改:
有:
演示:
列表理解可能是最有效的:
请注意,效率低下的Python级循环是不可避免的。
apply
+lambda
、map
+lambda
或就地解决方案都涉及Python级别的循环。在将要删除的所有元素存储在列表中。在
相关问题 更多 >
编程相关推荐