日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pandas中文官档 ~ 基础用法1

發布時間:2024/9/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pandas中文官档 ~ 基础用法1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

呆鳥云:“在學習 Python 數據分析的過程中,呆鳥發現直接看官檔就是牛逼啊,內容全面、豐富、詳細,而 Python 數據分析里最核心的莫過于 pandas,于是就想翻譯 pandas 官檔,于是就發現了?pypandas.cn?這個項目,于是就加入了 pandas 中文官檔翻譯小組,于是就沒時間更新公眾號,于是就犯懶想把翻譯與校譯的 pandas 當公眾號文章發上來,于是今后大家就可以在這里看了。”

本節介紹 pandas 數據結構的基礎用法。下列代碼創建示例數據對象:

In [1]: index = pd.date_range('1/1/2000', periods=8)In [2]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])In [3]: df = pd.DataFrame(np.random.randn(8, 3), index=index,...: columns=['A', 'B', 'C'])...:

Head 與 Tail

head()?與?tail()?用于快速預覽 Series 與 DataFrame,默認顯示 5 條數據,也可以指定要顯示的數量。

In [4]: long_series = pd.Series(np.random.randn(1000))In [5]: long_series.head() Out[5]: 0 -1.157892 1 -1.344312 2 0.844885 3 1.075770 4 -0.109050 dtype: float64In [6]: long_series.tail(3) Out[6]: 997 -0.289388 998 -1.020544 999 0.589993 dtype: float64

屬性與底層數據

Pandas 可以通過多個屬性訪問元數據:

  • shape:
    • 輸出對象的軸維度,與 ndarray 一致
  • 軸標簽
    • Series:?Index?(僅有此軸)
    • DataFrame:?Index?(行) 與列

注意:?為屬性賦值是安全的!

In [7]: df[:2] Out[7]:A B C 2000-01-01 -0.173215 0.119209 -1.044236 2000-01-02 -0.861849 -2.104569 -0.494929In [8]: df.columns = [x.lower() for x in df.columns]In [9]: df Out[9]:a b c 2000-01-01 -0.173215 0.119209 -1.044236 2000-01-02 -0.861849 -2.104569 -0.494929 2000-01-03 1.071804 0.721555 -0.706771 2000-01-04 -1.039575 0.271860 -0.424972 2000-01-05 0.567020 0.276232 -1.087401 2000-01-06 -0.673690 0.113648 -1.478427 2000-01-07 0.524988 0.404705 0.577046 2000-01-08 -1.715002 -1.039268 -0.370647

Pandas 對象(Index,?Series,?DataFrame)相當于數組的容器,用于存儲數據,并執行計算。大部分類型的底層數組都是?numpy.ndarray。不過,pandas 與第三方支持庫一般都會擴展 Numpy 類型系統,添加自定義數組(見數據類型)。

獲取?Index?或?Series?里的數據,請用?.array?屬性。

In [10]: s.array Out[10]: <PandasArray> [ 0.4691122999071863, -0.2828633443286633, -1.5090585031735124,-1.1356323710171934, 1.2121120250208506] Length: 5, dtype: float64In [11]: s.index.array Out[11]: <PandasArray> ['a', 'b', 'c', 'd', 'e'] Length: 5, dtype: object

array?一般指?ExtensionArray。至于什么是?ExtensionArray?及 pandas 為什么要用?ExtensionArray?不是本節要說明的內容。更多信息請參閱數據類型。

提取 Numpy 數組,用?to_numpy()?或?numpy.asarray()。

In [12]: s.to_numpy() Out[12]: array([ 0.4691, -0.2829, -1.5091, -1.1356, 1.2121])In [13]: np.asarray(s) Out[13]: array([ 0.4691, -0.2829, -1.5091, -1.1356, 1.2121])

Series?與?Index?的類型是?ExtensionArray?時,?to_numpy()?會復制數據,并強制轉換值。詳情見數據類型。

to_numpy()?可以控制?numpy.ndarray?生成的數據類型。以帶時區的 datetime 為例,Numpy 未提供時區信息的 datetime 數據類型,pandas 則提供了兩種表現形式:

  • 一種是帶?Timestamp?的?numpy.ndarray,提供了正確的?tz?信息。

  • 另一種是?datetime64[ns],這也是?numpy.ndarray,值被轉換為 UTC,但去掉了時區信息。

  • 時區信息可以用?dtype=object?保存。

    In [14]: ser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))In [15]: ser.to_numpy(dtype=object) Out[15]: array([Timestamp('2000-01-01 00:00:00+0100', tz='CET', freq='D'),Timestamp('2000-01-02 00:00:00+0100', tz='CET', freq='D')],dtype=object)

    或用?dtype='datetime64[ns]'?去除。

    In [16]: ser.to_numpy(dtype="datetime64[ns]") Out[16]: array(['1999-12-31T23:00:00.000000000', '2000-01-01T23:00:00.000000000'],dtype='datetime64[ns]')

    獲取?DataFrame?里的原數據略顯復雜。DataFrame 里所有列的數據類型都一樣時,DataFrame.to_numpy()?返回底層數據:

    In [17]: df.to_numpy() Out[17]: array([[-0.1732, 0.1192, -1.0442],[-0.8618, -2.1046, -0.4949],[ 1.0718, 0.7216, -0.7068],[-1.0396, 0.2719, -0.425 ],[ 0.567 , 0.2762, -1.0874],[-0.6737, 0.1136, -1.4784],[ 0.525 , 0.4047, 0.577 ],[-1.715 , -1.0393, -0.3706]])

    DataFrame 為同質型數據時,pandas 直接修改原始?ndarray,所做修改會直接反應在數據結構里。對于異質型數據,即 DataFrame 列的數據類型不一樣時,就不是這種操作模式了。與軸標簽不同,不能為值的屬性賦值。

    ::: tip 注意

    處理異質型數據時,輸出結果?ndarray?的數據類型適用于涉及的各類數據。若 DataFrame 里包含字符串,輸出結果的數據類型就是?object。要是只有浮點數或整數,則輸出結果的數據類型是浮點數。

    :::

    以前,pandas 推薦用?Series.values?或?DataFrame.values?從 Series 或 DataFrame 里提取數據。舊有代碼庫或在線教程里仍在用這種操作,但其實 pandas 已經對此做出了改進,現在推薦用?.array?或?to_numpy?這兩種方式提取數據,別再用?.values?了。.values?有以下幾個缺點:

  • Series 含擴展類型時,Series.values?無法判斷到底是該返回 Numpy?array,還是返回?ExtensionArray。而?Series.array?則只返回?ExtensionArray,且不會復制數據。Series.to_numpy()?則返回 Numpy 數組,其代價是需要復制、并強制轉換數據的值。

  • DataFrame 含多種數據類型時,DataFrame.values?會復制數據,并將數據的值強制轉換同一種數據類型,這是一種代價較高的操作。DataFrame.to_numpy()?則返回 Numpy 數組,這種方式更清晰,也不會把 DataFrame 里的數據都當作一種類型。

  • 加速操作

    借助?numexpr?與?bottleneck?支持庫,pandas 可以加速特定類型的二進制數值與布爾操作。

    處理大型數據集時,這兩個支持庫特別有用,加速效果也非常明顯。?numexpr?使用智能分塊、緩存與多核技術。bottleneck?是一組專屬 cython 例程,處理含?nans?值的數組時,特別快。

    請看下面這個例子(DataFrame?包含 100 列 X 10 萬行數據):

    操作0.11.0版 (ms)舊版 (ms)提升比率
    df1 > df213.32125.350.1063
    df1 * df221.7136.630.5928
    df1 + df222.0436.500.6039

    強烈建議安裝這兩個支持庫,了解更多信息,請參閱推薦支持庫。

    這兩個支持庫默認為啟用狀態,可用以下選項設置:

    0.20.0 版新增

    pd.set_option('compute.use_bottleneck', False) pd.set_option('compute.use_numexpr', False)

    二進制操作

    pandas 數據結構之間執行二進制操作,要注意下列兩個關鍵點:
    • 多維(DataFrame)與低維(Series)對象之間的廣播機制;
    • 計算中的缺失值處理。

    這兩個問題可以同時處理,但下面先介紹怎么分開處理。

    匹配/廣播機制

    DataFrame 支持?add()、sub()、mul()、div()?及?radd()、rsub()?等方法執行二進制操作。廣播機制重點關注輸入的 Series。通過?axis?關鍵字,匹配?index?或?columns?即可調用這些函數。

    In [18]: df = pd.DataFrame({....: 'one': pd.Series(np.random.randn(3), index=['a', 'b', 'c']),....: 'two': pd.Series(np.random.randn(4), index=['a', 'b', 'c', 'd']),....: 'three': pd.Series(np.random.randn(3), index=['b', 'c', 'd'])})....:In [19]: df Out[19]:one two three a 1.394981 1.772517 NaN b 0.343054 1.912123 -0.050390 c 0.695246 1.478369 1.227435 d NaN 0.279344 -0.613172In [20]: row = df.iloc[1]In [21]: column = df['two']In [22]: df.sub(row, axis='columns') Out[22]:one two three a 1.051928 -0.139606 NaN b 0.000000 0.000000 0.000000 c 0.352192 -0.433754 1.277825 d NaN -1.632779 -0.562782In [23]: df.sub(row, axis=1) Out[23]:one two three a 1.051928 -0.139606 NaN b 0.000000 0.000000 0.000000 c 0.352192 -0.433754 1.277825 d NaN -1.632779 -0.562782In [24]: df.sub(column, axis='index') Out[24]:one two three a -0.377535 0.0 NaN b -1.569069 0.0 -1.962513 c -0.783123 0.0 -0.250933 d NaN 0.0 -0.892516In [25]: df.sub(column, axis=0) Out[25]:one two three a -0.377535 0.0 NaN b -1.569069 0.0 -1.962513 c -0.783123 0.0 -0.250933 d NaN 0.0 -0.892516

    還可以用 Series 對齊多重索引 DataFrame 的某一層級。

    In [26]: dfmi = df.copy()In [27]: dfmi.index = pd.MultiIndex.from_tuples([(1, 'a'), (1, 'b'),....: (1, 'c'), (2, 'a')],....: names=['first', 'second'])....:In [28]: dfmi.sub(column, axis=0, level='second') Out[28]:one two three first second 1 a -0.377535 0.000000 NaNb -1.569069 0.000000 -1.962513c -0.783123 0.000000 -0.250933 2 a NaN -1.493173 -2.385688

    Series 與 Index 還支持?divmod()?內置函數,該函數同時執行向下取整除與模運算,返回兩個與左側類型相同的元組。示例如下:

    In [29]: s = pd.Series(np.arange(10))In [30]: s Out[30]: 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 dtype: int64In [31]: div, rem = divmod(s, 3)In [32]: div Out[32]: 0 0 1 0 2 0 3 1 4 1 5 1 6 2 7 2 8 2 9 3 dtype: int64In [33]: rem Out[33]: 0 0 1 1 2 2 3 0 4 1 5 2 6 0 7 1 8 2 9 0 dtype: int64In [34]: idx = pd.Index(np.arange(10))In [35]: idx Out[35]: Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')In [36]: div, rem = divmod(idx, 3)In [37]: div Out[37]: Int64Index([0, 0, 0, 1, 1, 1, 2, 2, 2, 3], dtype='int64')In [38]: rem Out[38]: Int64Index([0, 1, 2, 0, 1, 2, 0, 1, 2, 0], dtype='int64')

    divmod()?還支持元素級運算:

    In [39]: div, rem = divmod(s, [2, 2, 3, 3, 4, 4, 5, 5, 6, 6])In [40]: div Out[40]: 0 0 1 0 2 0 3 1 4 1 5 1 6 1 7 1 8 1 9 1 dtype: int64In [41]: rem Out[41]: 0 0 1 1 2 2 3 0 4 0 5 1 6 1 7 2 8 2 9 3 dtype: int64

    缺失值與填充缺失值操作

    Series 與 DataFrame 的算數函數支持?fill_value?選項,即用指定值替換某個位置的缺失值。比如,兩個 DataFrame 相加,除非兩個 DataFrame 里同一個位置都有缺失值,其相加的和仍為?NaN,如果只有一個 DataFrame 里存在缺失值,則可以用?fill_value?指定一個值來替代?NaN,當然,也可以用?fillna?把?NaN?替換為想要的值。

    In [42]: df Out[42]:one two three a 1.394981 1.772517 NaN b 0.343054 1.912123 -0.050390 c 0.695246 1.478369 1.227435 d NaN 0.279344 -0.613172In [43]: df2 Out[43]:one two three a 1.394981 1.772517 1.000000 b 0.343054 1.912123 -0.050390 c 0.695246 1.478369 1.227435 d NaN 0.279344 -0.613172In [44]: df + df2 Out[44]:one two three a 2.789963 3.545034 NaN b 0.686107 3.824246 -0.100780 c 1.390491 2.956737 2.454870 d NaN 0.558688 -1.226343In [45]: df.add(df2, fill_value=0) Out[45]:one two three a 2.789963 3.545034 1.000000 b 0.686107 3.824246 -0.100780 c 1.390491 2.956737 2.454870 d NaN 0.558688 -1.226343

    比較操作

    與上一小節的算數運算類似,Series 與 DataFrame 還支持?eq、ne、lt、gt、le、ge?等二進制比較操作的方法:

    序號縮寫英文中文
    1eqequal to等于
    2nenot equal to不等于
    3ltless than小于
    4gtgreater than大于
    5leless than or equal to小于等于
    6gegreater than or equal to大于等于
    In [46]: df.gt(df2) Out[46]:one two three a False False False b False False False c False False False d False False FalseIn [47]: df2.ne(df) Out[47]:one two three a False False True b False False False c False False False d True False False

    這些操作生成一個與左側輸入對象類型相同的 pandas 對象,即,dtype 為?bool。這些?boolean?對象可用于索引操作,參閱布爾索引小節。

    布爾簡化

    empty、any()、all()、bool()?可以把數據匯總簡化至單個布爾值。

    In [48]: (df > 0).all() Out[48]: one False two True three False dtype: boolIn [49]: (df > 0).any() Out[49]: one True two True three True dtype: bool

    還可以進一步把上面的結果簡化為單個布爾值。

    In [50]: (df > 0).any().any() Out[50]: True

    通過?empty?屬性,可以驗證 pandas 對象是否為空。

    In [51]: df.empty Out[51]: FalseIn [52]: pd.DataFrame(columns=list('ABC')).empty Out[52]: True

    用?bool()?方法驗證單元素 pandas 對象的布爾值。

    In [53]: pd.Series([True]).bool() Out[53]: TrueIn [54]: pd.Series([False]).bool() Out[54]: FalseIn [55]: pd.DataFrame([[True]]).bool() Out[55]: TrueIn [56]: pd.DataFrame([[False]]).bool() Out[56]: False

    ::: danger 警告

    以下代碼:

    >>> if df: ... pass

    >>> df and df2

    上述代碼試圖比對多個值,因此,這兩種操作都會觸發錯誤:

    ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().

    :::

    了解詳情,請參閱各種坑小節的內容。

    比較對象是否等效

    一般情況下,多種方式都能得出相同的結果。以?df + df?與?df * 2?為例。應用上一小節學到的知識,測試這兩種計算方式的結果是否一致,一般人都會用?(df + df == df * 2).all(),不過,這個表達式的結果是?False:

    In [57]: df + df == df * 2 Out[57]:one two three a True True False b True True True c True True True d False True TrueIn [58]: (df + df == df * 2).all() Out[58]: one False two True three False dtype: bool

    注意:布爾型 DataFrame df + df == df * 2?中有?False?值!這是因為兩個?NaN?值的比較結果為不等:

    In [59]: np.nan == np.nan Out[59]: False

    為了驗證數據是否等效,Series 與 DataFrame 等 N 維框架提供了?equals()?方法,,用這個方法驗證?NaN?值的結果為相等。

    In [60]: (df + df).equals(df * 2) Out[60]: True

    注意:Series 與 DataFrame 索引的順序必須一致,驗證結果才能為?True:

    In [61]: df1 = pd.DataFrame({'col': ['foo', 0, np.nan]})In [62]: df2 = pd.DataFrame({'col': [np.nan, 0, 'foo']}, index=[2, 1, 0])In [63]: df1.equals(df2) Out[63]: FalseIn [64]: df1.equals(df2.sort_index()) Out[64]: True

    比較 array 型對象

    用標量值與 pandas 數據結構對比數據元素非常簡單:

    In [65]: pd.Series(['foo', 'bar', 'baz']) == 'foo' Out[65]: 0 True 1 False 2 False dtype: boolIn [66]: pd.Index(['foo', 'bar', 'baz']) == 'foo' Out[66]: array([ True, False, False])

    pandas 還能對比兩個等長 array 對象里的數據元素:

    In [67]: pd.Series(['foo', 'bar', 'baz']) == pd.Index(['foo', 'bar', 'qux']) Out[67]: 0 True 1 True 2 False dtype: boolIn [68]: pd.Series(['foo', 'bar', 'baz']) == np.array(['foo', 'bar', 'qux']) Out[68]: 0 True 1 True 2 False dtype: bool

    對比不等長的?Index?或?Series?對象會觸發?ValueError:

    In [55]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo', 'bar']) ValueError: Series lengths must match to compareIn [56]: pd.Series(['foo', 'bar', 'baz']) == pd.Series(['foo']) ValueError: Series lengths must match to compare

    注意:這里的操作與 Numpy 的廣播機制不同:

    In [69]: np.array([1, 2, 3]) == np.array([2]) Out[69]: array([False, True, False])

    Numpy 無法執行廣播操作時,返回?False:

    In [70]: np.array([1, 2, 3]) == np.array([1, 2]) Out[70]: False

    合并重疊數據集

    有時會合并兩個近似數據集,兩個數據集中,其中一個的數據比另一個多。比如,展示特定經濟指標的兩個數據序列,其中一個是“高質量”指標,另一個是“低質量”指標。一般來說,低質量序列可能包含更多的歷史數據,或覆蓋更廣的數據。因此,要合并這兩個 DataFrame 對象,其中一個 DataFrame 中的缺失值將按指定條件用另一個 DataFrame 里類似標簽中的數據進行填充。要實現這一操作,請用下列代碼中的?combine_first()?函數。

    In [71]: df1 = pd.DataFrame({'A': [1., np.nan, 3., 5., np.nan],....: 'B': [np.nan, 2., 3., np.nan, 6.]})....:In [72]: df2 = pd.DataFrame({'A': [5., 2., 4., np.nan, 3., 7.],....: 'B': [np.nan, np.nan, 3., 4., 6., 8.]})....:In [73]: df1 Out[73]:A B 0 1.0 NaN 1 NaN 2.0 2 3.0 3.0 3 5.0 NaN 4 NaN 6.0In [74]: df2 Out[74]:A B 0 5.0 NaN 1 2.0 NaN 2 4.0 3.0 3 NaN 4.0 4 3.0 6.0 5 7.0 8.0In [75]: df1.combine_first(df2) Out[75]:A B 0 1.0 NaN 1 2.0 2.0 2 3.0 3.0 3 5.0 4.0 4 3.0 6.0 5 7.0 8.0

    通用的 DataFrame 合并方法

    上述?combine_first()?方法調用了更普適的?DataFrame.combine()?方法。該方法提取另一個 DataFrame 及合并器函數,并將之與輸入的 DataFrame 對齊,再傳遞與 Series 配對的合并器函數(比如,名稱相同的列)。

    下面的代碼復現了上述的?combine_first()?函數:

    In [76]: def combiner(x, y):....: return np.where(pd.isna(x), y, x)....:

    點擊閱讀原文跳轉原文

    總結

    以上是生活随笔為你收集整理的Pandas中文官档 ~ 基础用法1的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。