如何在熊猫表中放置重复项?

时间:2019-10-14 10:48:10

标签: python pandas duplicates

我对编码非常陌生,并且希望代码能够计算单词的出现频率,但是由于我不确定如何删除重复项,所以我停了下来。

txt = " remember all those walls we built remember those times"
words = txt.split()
for word in words:
    print (word + " " + str(txt.count(word)))
import pandas as pd
my_table = pd.DataFrame()
for word in words:
    tempdf = pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]})
    my_table = my_table.append(tempdf)
print(my_table)

3 个答案:

答案 0 :(得分:1)

您需要:

txt = " remember all those walls we built remember those times"

words = txt.split()

for word in words:

    print(word + " " + str(txt.count(word)))

import pandas as pd

mytable = pd.DataFrame()

for word in words:

    tempdf = pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]})
    mytable = mytable.append(tempdf)

print(mytable)

,或者使用pd.concat更好:

import pandas as pd
txt = " remember all those walls we built remember those times"
words = txt.split()
for word in words:
    print(word + " " + str(txt.count(word)) )

my_table=pd.concat([pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]}) for word in words])
print(mytable)

请记住,您也可以update字典,然后在最后创建数据框

答案 1 :(得分:0)

您需要在第4行中添加一个额外的结束括号 并在第5行中将导入熊猫添加为pd,因为您使用的是pd而不是熊猫

答案 2 :(得分:0)

您的语法错误是由于在{em> )之前的行printimport pandas as pd的右括号中缺少了print(word + " " + str(txt.count(word))) 。该行应显示为:

mouseWheel(event: WheelEvent) {
    const max_scale = 4;
    const con = this.svgElement.children[0];
    const wheel = event.deltaY < 0 ? 1 : -1;
    if ( (event.deltaY < 0 ? this.zoom + Math.exp(wheel * this.zoomIntensity) :  this.zoom - Math.exp(wheel * this.zoomIntensity)) > 0) {
    this.zoom = event.deltaY < 0 ? this.zoom + Math.exp(wheel * this.zoomIntensity) :  this.zoom - Math.exp(wheel * this.zoomIntensity);
    this.originx = this.currentPosition.x ;
    this.originy = this.currentPosition.y ;
    con.setAttribute('transform', `translate(${this.originx}, ${this.originy})
      scale(${this.zoom}, ${this.zoom})
      translate(${-this.originx}, ${-this.originy})`);
    this.scale += wheel * this.zoomIntensity * this.scale;
    this.scale = Math.max(1, Math.min(max_scale, this.scale));
    this.visibleWidth = this.width / this.scale;
    this.visibleHeight = this.height / this.scale;


    }
  }

作为语法错误的一般提示,请首先检查前一行或上一个函数调用中是否缺少括号或多余的括号。