1. <em id="yud1w"><acronym id="yud1w"><u id="yud1w"></u></acronym></em>
      
      
      <button id="yud1w"></button>

      python教程

      當前位置:首頁?>?Pandas教程?>?當前文章

      Pandas教程

      pandas坑:創建df后多行數據重復

      2021-11-23 179贊 老董筆記
      每篇文章努力于解決一個問題!更多精品可移步文章底部。

        字典可以創建成df數據結構,但是必須傳參index,否則會報錯ValueError: If using all scalar values, you must pass an index。如下:

      # -*- coding:utf-8 -*-
      dic = {'domain': 'www.bdd33.com', 'author': 'dong',}
      df = pd.DataFrame(dic)
      print(df)
      
      raise ValueError("If using all scalar values, you must pass an index")
      ValueError: If using all scalar values, you must pass an index
      

        如果傳參寫成了標量的形式還會報錯Index(...) must be called with a collection of some kind, 0 was passed。如下:

      # -*- coding:utf-8 -*-
      dic = {'domain': 'www.bdd33.com', 'author': 'dong',}
      df = pd.DataFrame(dic,index=0)
      print(df)
      
      raise cls._scalar_data_error(data)
      TypeError: Index(...) must be called with a collection of some kind, 0 was passed
      

        如果傳參寫成了列表的形式則沒問題

      # -*- coding:utf-8 -*-
      
      dic = {'domain': 'www.bdd33.com', 'author': 'dong',}
      df = pd.DataFrame(dic,index=[0])
      print(df)
      
                   domain author
      0  www.bdd33.com   dong
      

        如果傳參的列表不是單個元素而是多個,結果則是重復出現同1行數據,雖然不報錯,我覺得這是個坑

      # -*- coding:utf-8 -*-
      
      dic = {'domain': 'www.bdd33.com', 'author': 'dong',}
      df = pd.DataFrame(dic,index=[0,1,2])
      print(df)
      
                   domain author
      0  www.bdd33.com   dong
      1  www.bdd33.com   dong
      2  www.bdd33.com   dong
      

      文章評論

      pandas坑:創建df后多行數據重復文章寫得不錯,值得贊賞
      国产99视频精品免视看6