<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Pandas是基于NumPy的一個非常好用的庫,正如名字一樣,人見人愛。之所以如此,就在于不論是讀取、處理數據,用它都非常簡單。 ## [](https://github.com/qiwsir/StarterLearningPython/blob/master/311.md#基本的數據結構)基本的數據結構 Pandas有兩種自己獨有的基本數據結構。讀者應該注意的是,它固然有著兩種數據結構,因為它依然是python的一個庫,所以,python中有的數據類型在這里依然適用,也同樣還可以使用類自己定義數據類型。只不過,Pandas里面又定義了兩種數據類型:Series和DataFrame,它們讓數據操作更簡單了。 以下操作都是基于: [![](https://box.kancloud.cn/2015-09-07_55ed604ebfad3.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31101.png) 為了省事,后面就不在顯示了。并且如果你跟我一樣是使用ipython notebook,只需要開始引入模塊即可。 ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/311.md#series)Series Series就如同列表一樣,一系列數據,每個數據對應一個索引值。比如這樣一個列表:[9, 3, 8],如果跟索引值寫到一起,就是: | data | 9 | 3 | 8 | | --- | --- | --- | --- | | index | 0 | 1 | 2 | 這種樣式我們已經熟悉了,不過,在有些時候,需要把它豎過來表示: | index | data | | --- | --- | | 0 | 9 | | 1 | 3 | | 2 | 8 | 上面兩種,只是表現形式上的差別罷了。 Series就是“豎起來”的list: [![](https://box.kancloud.cn/2015-09-07_55ed6055d681e.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31102.png) 另外一點也很像列表,就是里面的元素的類型,由你任意決定(其實是由需要來決定)。 這里,我們實質上創建了一個Series對象,這個對象當然就有其屬性和方法了。比如,下面的兩個屬性依次可以顯示Series對象的數據值和索引: [![](https://box.kancloud.cn/2015-09-07_55ed60578c355.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31103.png) 列表的索引只能是從0開始的整數,Series數據類型在默認情況下,其索引也是如此。不過,區別于列表的是,Series可以自定義索引: [![](https://box.kancloud.cn/2015-09-07_55ed60593194e.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31104.png) [![](https://box.kancloud.cn/2015-09-07_55ed605c48056.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31105.png) 自定義索引,的確比較有意思。就憑這個,也是必須的。 每個元素都有了索引,就可以根據索引操作元素了。還記得list中的操作嗎?Series中,也有類似的操作。先看簡單的,根據索引查看其值和修改其值: [![](https://box.kancloud.cn/2015-09-07_55ed605ee13da.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31106.png) 這是不是又有點類似dict數據了呢?的確如此。看下面就理解了。 讀者是否注意到,前面定義Series對象的時候,用的是列表,即Series()方法的參數中,第一個列表就是其數據值,如果需要定義index,放在后面,依然是一個列表。除了這種方法之外,還可以用下面的方法定義Series對象: [![](https://box.kancloud.cn/2015-09-07_55ed606294729.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31108.png) 現在是否理解為什么前面那個類似dict了?因為本來就是可以這樣定義的。 這時候,索引依然可以自定義。Pandas的優勢在這里體現出來,如果自定義了索引,自定的索引會自動尋找原來的索引,如果一樣的,就取原來索引對應的值,這個可以簡稱為“自動對齊”。 [![](https://box.kancloud.cn/2015-09-07_55ed6064b1970.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31110.png) 在sd中,只有`'python':8000, 'c++':8100, 'c#':4000`,沒有"java",但是在索引參數中有,于是其它能夠“自動對齊”的照搬原值,沒有的那個"java",依然在新Series對象的索引中存在,并且自動為其賦值`NaN`。在Pandas中,如果沒有值,都對齊賦給`NaN`。來一個更特殊的: [![](https://box.kancloud.cn/2015-09-07_55ed606e55f24.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31109.png) 新得到的Series對象索引與sd對象一個也不對應,所以都是`NaN`。 Pandas有專門的方法來判斷值是否為空。 [![](https://box.kancloud.cn/2015-09-07_55ed606ff3ebe.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31111.png) 此外,Series對象也有同樣的方法: [![](https://box.kancloud.cn/2015-09-07_55ed6071f11e5.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31112.png) 其實,對索引的名字,是可以從新定義的: [![](https://box.kancloud.cn/2015-09-07_55ed6078bba9c.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31117.png) 對于Series數據,也可以做類似下面的運算(關于運算,后面還要詳細介紹): [![](https://box.kancloud.cn/2015-09-07_55ed608508a05.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31107.png) [![](https://box.kancloud.cn/2015-09-07_55ed608695231.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31113.png) 上面的演示中,都是在ipython notebook中進行的,所以截圖了。在學習Series數據類型同時了解了ipyton notebook。對于后面的所有操作,讀者都可以在ipython notebook中進行。但是,我的講述可能會在python交互模式中進行。 ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/311.md#dataframe)DataFrame DataFrame是一種二維的數據結構,非常接近于電子表格或者類似mysql數據庫的形式。它的豎行稱之為columns,橫行跟前面的Series一樣,稱之為index,也就是說可以通過columns和index來確定一個主句的位置。(有人把 DataFrame翻譯為“數據框”,是不是還可以稱之為“筐”呢?向里面裝數據嘛。) [![](https://box.kancloud.cn/2015-09-07_55ed608e23efe.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/3images/31118.png) 下面的演示,是在python交互模式下進行,讀者仍然可以在ipython notebook環境中測試。 ~~~ >>> import pandas as pd >>> from pandas import Series, DataFrame >>> data = {"name":["yahoo","google","facebook"], "marks":[200,400,800], "price":[9, 3, 7]} >>> f1 = DataFrame(data) >>> f1 marks name price 0 200 yahoo 9 1 400 google 3 2 800 facebook 7 ~~~ 這是定義一個DataFrame對象的常用方法——使用dict定義。字典的“鍵”("name","marks","price")就是DataFrame的columns的值(名稱),字典中每個“鍵”的“值”是一個列表,它們就是那一豎列中的具體填充數據。上面的定義中沒有確定索引,所以,按照慣例(Series中已經形成的慣例)就是從0開始的整數。從上面的結果中很明顯表示出來,這就是一個二維的數據結構(類似excel或者mysql中的查看效果)。 上面的數據顯示中,columns的順序沒有規定,就如同字典中鍵的順序一樣,但是在DataFrame中,columns跟字典鍵相比,有一個明顯不同,就是其順序可以被規定,向下面這樣做: ~~~ >>> f2 = DataFrame(data, columns=['name','price','marks']) >>> f2 name price marks 0 yahoo 9 200 1 google 3 400 2 facebook 7 800 ~~~ 跟Series類似的,DataFrame數據的索引也能夠自定義。 ~~~ >>> f3 = DataFrame(data, columns=['name', 'price', 'marks', 'debt'], index=['a','b','c','d']) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/pymodules/python2.7/pandas/core/frame.py", line 283, in __init__ mgr = self._init_dict(data, index, columns, dtype=dtype) File "/usr/lib/pymodules/python2.7/pandas/core/frame.py", line 368, in _init_dict mgr = BlockManager(blocks, axes) File "/usr/lib/pymodules/python2.7/pandas/core/internals.py", line 285, in __init__ self._verify_integrity() File "/usr/lib/pymodules/python2.7/pandas/core/internals.py", line 367, in _verify_integrity assert(block.values.shape[1:] == mgr_shape[1:]) AssertionError ~~~ 報錯了。這個報錯信息就太不友好了,也沒有提供什么線索。這就是交互模式的不利之處。修改之,錯誤在于index的值——列表——的數據項多了一個,data中是三行,這里給出了四個項(['a','b','c','d'])。 ~~~ >>> f3 = DataFrame(data, columns=['name', 'price', 'marks', 'debt'], index=['a','b','c']) >>> f3 name price marks debt a yahoo 9 200 NaN b google 3 400 NaN c facebook 7 800 NaN ~~~ 讀者還要注意觀察上面的顯示結果。因為在定義f3的時候,columns的參數中,比以往多了一項('debt'),但是這項在data這個字典中并沒有,所以debt這一豎列的值都是空的,在Pandas中,空就用NaN來代表了。 定義DataFrame的方法,除了上面的之外,還可以使用“字典套字典”的方式。 ~~~ >>> newdata = {"lang":{"firstline":"python","secondline":"java"}, "price":{"firstline":8000}} >>> f4 = DataFrame(newdata) >>> f4 lang price firstline python 8000 secondline java NaN ~~~ 在字典中就規定好數列名稱(第一層鍵)和每橫行索引(第二層字典鍵)以及對應的數據(第二層字典值),也就是在字典中規定好了每個數據格子中的數據,沒有規定的都是空。 ~~~ >>> DataFrame(newdata, index=["firstline","secondline","thirdline"]) lang price firstline python 8000 secondline java NaN thirdline NaN NaN ~~~ 如果額外確定了索引,就如同上面顯示一樣,除非在字典中有相應的索引內容,否則都是NaN。 前面定義了DataFrame數據(可以通過兩種方法),它也是一種對象類型,比如變量f3引用了一個對象,它的類型是DataFrame。承接以前的思維方法:對象有屬性和方法。 ~~~ >>> f3.columns Index(['name', 'price', 'marks', 'debt'], dtype=object) ~~~ DataFrame對象的columns屬性,能夠顯示素有的columns名稱。并且,還能用下面類似字典的方式,得到某豎列的全部內容(當然包含索引): ~~~ >>> f3['name'] a yahoo b google c facebook Name: name ~~~ 這是什么?這其實就是一個Series,或者說,可以將DataFrame理解為是有一個一個的Series組成的。 一直耿耿于懷沒有數值的那一列,下面的操作是統一給那一列賦值: ~~~ >>> f3['debt'] = 89.2 >>> f3 name price marks debt a yahoo 9 200 89.2 b google 3 400 89.2 c facebook 7 800 89.2 ~~~ 除了能夠統一賦值之外,還能夠“點對點”添加數值,結合前面的Series,既然DataFrame對象的每豎列都是一個Series對象,那么可以先定義一個Series對象,然后把它放到DataFrame對象中。如下: ~~~ >>> sdebt = Series([2.2, 3.3], index=["a","c"]) #注意索引 >>> f3['debt'] = sdebt ~~~ 將Series對象(sdebt變量所引用) 賦給f3['debt']列,Pandas的一個重要特性——自動對齊——在這里起做用了,在Series中,只有兩個索引("a","c"),它們將和DataFrame中的索引自動對齊。于是乎: ~~~ >>> f3 name price marks debt a yahoo 9 200 2.2 b google 3 400 NaN c facebook 7 800 3.3 ~~~ 自動對齊之后,沒有被復制的依然保持NaN。 還可以更精準的修改數據嗎?當然可以,完全仿照字典的操作: ~~~ >>> f3["price"]["c"]= 300 >>> f3 name price marks debt a yahoo 9 200 2.2 b google 3 400 NaN c facebook 300 800 3.3 ~~~ 這些操作是不是都不陌生呀,這就是Pandas中的兩種數據對象。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看