Python使用Pandas对列表赋值循环该怎么办??

毕设项目助手 论文问答 1
def fuzhi(x):
    i=0
    for x in df3['str']:
        x==i
        i=i+1
        print(i)
        if i>=len(df3['str']):                                                                                                                   
            break
df3.loc[:,"strlist"] = df3.apply(fuzhi,axis=1)
用这段代码,陷入了死循环

该怎么实现下边这样的效果,也就是生成strlist这一列,比如第一个数据a,他就是0,然后再次出现在str这一列,他还是0,以此类推;b是1,再次出现在str这一列,还是1

回复

共2条回复 我来回复
  • 源码客栈网
    这个人很懒,什么都没有留下~
    评论
    import pandas as pd
    from pandas.core.frame import DataFrame
    
    def getValue(rawList):
      num = 0
      newDict = {}
      strList = []
      for i in rawList:
        if i not in strList:
          newDict[i] = num
          strList.append(i)
          num += 1
    
      resultList = [newDict[b] for b in rawList]
      newDataFrame = DataFrame({"str":rawList, "strlist":resultList})
      print(newDataFrame)
    #data = pd.read_table("test")
    #将你的str列转换成列表
    
    rawList = data['str'].tolist()
    #getValue(['a', 'b', 'c', 'a', 'd', 'c', 'b', 'a', 'e'])
    getValue(rawList)
    
    0条评论
  • 源码工坊
    这个人很懒,什么都没有留下~
    评论
    import pandas as pd
    from pandas.core.frame import DataFrame
    
    def getValue(rawList):
      num = 0
      newDict = {}
      strList = []
      for i in rawList:
        if i not in strList:
          newDict[i] = num
          strList.append(i)
          num += 1
    
      resultList = [newDict[b] for b in rawList]
      newDataFrame = DataFrame({"str":rawList, "strlist":resultList})
      print(newDataFrame)
    #data = pd.read_table("test")
    #将你的str列转换成列表
    
    rawList = data['str'].tolist()
    #getValue(['a', 'b', 'c', 'a', 'd', 'c', 'b', 'a', 'e'])
    getValue(rawList)
    
    0条评论

发表回复

登录后才能评论