python组成结构_Python数据分析丨pandas基本数据结构组成
本文的文字及圖片來源于網(wǎng)絡,僅供學習、交流使用,不具有任何商業(yè)用途,版權歸原作者所有,如有問題請及時聯(lián)系我們以作處理
以下文章來源于騰訊云,作者:統(tǒng)計學家
目錄
1引言
2 Series數(shù)組
2.1 Series數(shù)組構成
2.2 創(chuàng)建Series數(shù)組
2.3 Series數(shù)組常用屬性
3 DataFrame數(shù)組
3.1 DataFrame數(shù)組構成
3.2 創(chuàng)建DataFrame數(shù)組
3.3 DataFrame數(shù)組的常用屬性
4 總結
1引言
本文總結Pandas中兩種常用的數(shù)據(jù)類型:
(1)Series是一種一維的帶標簽數(shù)組對象。
(2)DataFrame,二維,Series容器
2 Series數(shù)組
2.1 Series數(shù)組構成
Series數(shù)組對象由兩部分構成:
值(value):一維數(shù)組的各元素值,是一個ndarray類型數(shù)據(jù)。索引(index):與一維數(shù)組值一一對應的標簽。利用索引,我們可非常方便得在Series數(shù)組中進行取值。如下所示,我們通過字典創(chuàng)建了一個Series數(shù)組,輸出結果的第一列就是索引,第二列就是數(shù)組的具體值。
>>> import pandas as pd
>>> a =pd.Series([102, 212, 332, 434])
>>> a
0 102
1 212
2 332
3 434
dtype: int64
也可以在創(chuàng)建時手動指定索引:
>>> a = pd.Series([102, 212, 332, 434], index=['第一列', '第二列', '第三列', '第四列'])
>>> a
第一列 102
第二列 212
第三列 332
第四列 434
dtype: int64
利用索引,我們可以更加方便得在數(shù)組中進行取值:
>>> a['第一列']
102
>>> a[['第一列', '第二列']]
第一列 102
第二列 212
dtype: int64
當然,你也可以使用以往的數(shù)字下標從數(shù)組中取值:
>>> a[0]
102
>>> a[[0,1]]
第一列 102
第二列 212
dtype: int64
2.2 創(chuàng)建Series數(shù)組
(1)通過list、tuple創(chuàng)建
>>> pd.Series([123, 321, 345,543]) # 傳入一個list
0 123
1 321
2 345
3 543
dtype: int64
>>> pd.Series((123, 321, 345,543)) # 傳入一個元組
0 123
1 321
2 345
3 543
dtype: int64
(2)通過傳入一維numpy數(shù)組對象創(chuàng)建
>>> import numpy as np
>>> n = np.arange(3) # 創(chuàng)建一個一維的numpy數(shù)組
>>> pd.Series(n)
0 0
1 1
2 2
dtype: int32
注意:傳入的numpy必須是一維的數(shù)組,否則會報錯。
>>> n = np.arange(6).reshape((2,3))
>>> pd.Series(n)
Traceback (most recent call last):
File "", line 1, in
……
packages\pandas\core\internals\construction.py", line 729, in sanitize_array
raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional
(3)通過傳入字典創(chuàng)建
通過字典創(chuàng)建Series數(shù)組時,字典的key會自動被設置成Series數(shù)組的索引:
>>> pd.Series({'name':'張三', 'age':40, 'weight':140})
name 張三
age 40
weight 140
dtype: object
4)通過傳入一個標量值創(chuàng)建
當傳入一個標量值時,必須傳入index索引,Series會根據(jù)傳入的index參數(shù)來確定數(shù)組對象的長度:
>>> a = pd.Series(10, index=['a', 'b', 'c', 'd'])
>>> a
a 10
b 10
c 10
d 10
dtype: int64
2.3 Series數(shù)組常用屬性
Series數(shù)組的屬性與numpy數(shù)組屬性很是類似,如下表所示:
3 DataFrame數(shù)組
3.1 DataFrame數(shù)組構成
DataFrame數(shù)組是Pandas中另一種數(shù)據(jù)結構,其數(shù)據(jù)的呈現(xiàn)方式類似于Excel這種二維表結構。相比于Series數(shù)組,DataFrame可以存放多維數(shù)據(jù),所以DataFrame不僅僅有索引,還有列名,如下所示:
>>> d = {'one': [1, 2, 3, 4], 'two':['一', '二', '三', '四']}
>>> pd.DataFrame(d)
one two
0 1 一
1 2 二
2 3 三
3 4 四
>>> df.index
RangeIndex(start=0, stop=4, step=1)
>>> df.columns
Index(['one', 'two'], dtype='object')
可以看到,DataFrame數(shù)組可以包含多維數(shù)據(jù),類似于一張二維表。與Series類似,DataFrame數(shù)組也有一個index索引,在不指定索引時,通常會自動生成從零開始步長為1的索引。此外DataFrame數(shù)組還有一個列名,索引和列名是從數(shù)組中挑選數(shù)據(jù)的重要依據(jù)。
3.2 創(chuàng)建DataFrame數(shù)組
(1)通過字典創(chuàng)建
通過字典來創(chuàng)建DataFrame數(shù)組時,字典的鍵將會自動成DataFrame數(shù)組的列名,字典的值必須是可迭代對象,例如Series、numpy數(shù)組、list、tuple等,不同Series數(shù)組中對應的缺失值pandas將自動填充NaN:
以list列表為值的字典:
>>> d = {'one': [1, 2, 3, 4], 'two':['一', '二', '三', '四']}
>>> pd.DataFrame(d)
one two
0 1 一
1 2 二
2 3 三
3 4 四
以numpy數(shù)組為值得字典:
>>> d = {'zero': np.zeros((3,)), 'ones': np.ones((3,)), 'twos':np.full((3,),2)}
>>> pd.DataFrame(d)
zero ones twos
0 0.0 1.0 2
1 0.0 1.0 2
2 0.0 1.0 2
以Series為值的字典:
>>> d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
>>> df = pd.DataFrame(d) # 創(chuàng)建DataFrame數(shù)組
>>> df
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
無論是上面那種類型對象為值的字典,都可以通過下面的方式重新指定列索引:
>>> pd.DataFrame(d, index=['d', 'b', 'a'])
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0
當然,也可以在手動指定列名,不過行索引對應的鍵數(shù)據(jù)才會傳入新建的數(shù)組中:
>>> pd.DataFrame(d, index=['d', 'b', 'a'], columns=['two', 'three'])
two three
d 4.0 NaN
b 2.0 NaN
a 1.0 NaN
(2)通過列表創(chuàng)建
通過列表創(chuàng)建DataFrame數(shù)組時,列表的每一個元素必須是字典,這樣,字典的鍵將作為列名。
>>> d = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
>>> pd.DataFrame(d)
a b c
0 1 2 NaN
1 5 10 20.0
>>> pd.DataFrame(d, index=['第一行', '第二行']) # 重新指定索引
a b c
第一行 1 2 NaN
第二行 5 10 20.0
(3)通過功能函數(shù)創(chuàng)建
我們還可以通過諸如from_dict()、from_records()這類的功能函數(shù)來創(chuàng)建DataFrame數(shù)組,以from_dict()為例:
>>> d = {'A': [1, 2, 3], 'B': [4, 5, 6]}
>>> pd.DataFrame.from_dict(d)
A B
0 1 4
1 2 5
2 3 6
如果需要讓字典的鍵作為索引,重新指定列名,可以傳入orient='index’參數(shù),然后重新傳入列名:
>>> pd.DataFrame.from_dict(d,orient='index', columns=['one', 'two', 'three'])
one two three
A 1 2 3
B 4 5 6
3.3 DataFrame數(shù)組的常用屬性
DataFrame數(shù)組的屬性與Series數(shù)據(jù)幾乎一樣,只是多了一個保存列名信息的columns屬性,參看上面表格中的Series屬性就行了。
4 總結
本文大致介紹了Pandas中的兩種重要數(shù)據(jù)結構Series數(shù)組對象和DataFrame數(shù)組對象的特點、主要創(chuàng)建方法、屬性。
想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經(jīng)上傳群文件流,可以自行下載!還有海量最新2020python學習資料。
標簽:index,pd,Python,Series,DataFrame,索引,數(shù)組,數(shù)據(jù)結構,pandas
來源: https://www.cnblogs.com/aa1273935919/p/13950959.html
總結
以上是生活随笔為你收集整理的python组成结构_Python数据分析丨pandas基本数据结构组成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cass或cad里提取点坐标及高程的插件
- 下一篇: Gensee移动SDK之(一)结构组成