遍歷兩個pandas列 iterate through two pandas columns


使用for filePath,content in corpos.itertuples(index=False)遍歷兩行數據

問題:在遍歷pandas進行分詞時,並將修改后的文本寫入源文件

一開始使用for content in corpos['content'],

雖然content有遍歷,但是filePath在for循環中,始終停留在corpos的最后一行filepath,並未能遍歷成功。

經修改后:

#-------------------------------------------------建立corpos

corpos = pandas.DataFrame(columns=['filePath','content']

#-------------中間corpos存入數據的過程省略

#-------------分詞並修改文本

t='/'
for filePath,content in corpos.itertuples(index=False):
  f = codecs.open(filePath,'r','utf-8')
  cont=f.read()
  f.close()
  gai=''
  segs=jieba.cut(content)
  for seg in segs:
    if seg ==' ' or seg =="\r\n" or seg==' ' or seg=='':
    gai=gai+seg
  else:
    gai=gai+seg+t
  cont=gai[2:]
  f = codecs.open(filePath,'w','utf-8')
  f.write(cont)
  f.flush()
  f.close()

參考鏈接:http://stackoverflow.com/questions/15125343/how-to-iterate-through-two-pandas-columns

生活不易,本人有意向做數據分析兼職或python在線輔導,如有需要請聯系qq號1334832194。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM