iterrows 的矢量化替代方案:语义分析

时间:2021-05-20 07:14:27

标签: pandas algorithm dataframe numpy semantics

嗨,我目前正在进行语义推文分析,并希望使用 Numpy Vectorization 来改善我的代码运行时间。

我尝试增强我的代码一段时间,但没有成功。 我可以在循环迭代中将公式输入到函数中并通过 Numpy.vectorize 应用它吗?

body {
  color: #6a6c6f;
  background-color: #f1f3f6;
  margin-top:30px;
}

.container {
  max-width: 960px;
}


/* */

.panel-heading a:not(.collapsed) {
    border-bottom:1px solid red;
}

.panel-default>.panel-heading {
  color: #333;
  background-color: #fff;
  border-color: #e4e5e7;
  padding: 0;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.panel-default>.panel-heading a {
  display: block;
  padding: 10px 15px;
}

.panel-default>.panel-heading a:after {
  content: "";
  position: relative;
  top: 1px;
  display: inline-block;
  font-family: 'Glyphicons Halflings';
  font-style: normal;
  font-weight: 400;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  float: right;
  transition: transform .25s linear;
  -webkit-transition: -webkit-transform .25s linear;
}

.panel-default>.panel-heading a[aria-expanded="true"] {
  background-color: #eee;
}

.panel-default>.panel-heading a[aria-expanded="true"]:after {
  content: "\2212";
  -webkit-transform: rotate(180deg);
  transform: rotate(180deg);
}

.panel-default>.panel-heading a[aria-expanded="false"]:after {
  content: "\002b";
  -webkit-transform: rotate(90deg);
  transform: rotate(90deg);
}

.add-border-bottom{
  border-bottom:1px solid red;  
}

1 个答案:

答案 0 :(得分:0)

您可以使用 apply 函数,而不是遍历数据帧中的行。

def get_sentiments(text):
    score = ss.polarity_scores(text)
    neg = score["neg"]
    neu = score["neu"]
    pos = score["pos"]
    comp = score["compound"]
    if neg > pos:
        sentiment = "negative"
    elif pos > neg:
        sentiment = "positive"
    else:
        sentiment = "neutral"
    return sentiment,neg,neu,pos,comp
    
tw_list[["sentiment","neg","neu","pos","comp"]] = tw_list["full_text"].apply(get_sentiments,result_type='broadcast')

这应该会提高性能