遍历两个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。

转载于:https://www.cnblogs.com/chenyaling/p/5511773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值