[英]Non-looping way in Numpy to convert a string of letters into a boolean array (corresponding to each letter of the string)
[英]Is there a non-looping way to perform text searching in a data frame
我有一個巨大的 ngram 列表要搜索。 我想知道他們在我的歷史 dataframe 上的頻率以及我在我的歷史上的數字變量的平均值。 我有一個非常丑陋的方法來做這件事(可行),但是由於 ngram 列表很大,所以它真的很慢。
我想避免做循環,因為我猜這是我的速度問題的主要原因,但我不知道我該怎么做。
任何的想法?
output = pd.DataFrame()
ngrams = ['ngram1', 'ngram2', 'ngram3', ..., 'ngram350000']
for i in list(ngrams):
temp = pd.DataFrame(data={'ngram' : [i],
'count' : historic_df['text_variable'].str.contains(i, na=False).sum(),
'mean' : historic_df[historic_df['text_variable'].str.contains(i, na=False)]['numeric_variable'].mean()})
output = pd.concat([output, temp], axis=0)
試試 DataFrame.apply()
def func(x):
temp = pd.DataFrame(data={'ngram' : [i],
'count' : historic_df['text_variable'].str.contains(i, na=False).sum(),
'mean' : historic_df[historic_df['text_variable'].str.contains(i, na=False)]['numeric_variable'].mean()})
output = pd.concat([output, temp], axis=0)
return x
output = pd.DataFrame()
ngrams = pd.DataFrame({'ngram':['ngram1', 'ngram2', 'ngram3', ..., 'ngram350000']})
ngrams.apply(func)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.