[英]Slice a dataframe based on one column starting with the value of another column
我有一個名為data
的數據框,看起來像這樣:
|...|category|...|ngram|...|
我需要將此數據幀切片到category
以ngram
開頭的ngram
。 因此,例如,如果我有以下實例:
然后應從結果數據框中刪除該實例。
在T-SQL中,我使用以下查詢(這可能不是最好的方法,但它可以工作):
SELECT
*
FROM mytable
WHERE category NOT LIKE ngram+'%';
我已經閱讀了一些,我的最佳嘗試是:
data[data.category.str.startswith(data.ngram.str) == True]
但這不會返回任何行,也不會返回任何行(使用== True
)
#use df.apply to filter the rows with category starts with ngram.
data[data.apply(lambda x: x.category.startswith(x.ngram), axis=1)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.