DataFrame的構造


DateFrame

  1. DateFrame:一個二維標記數據結構,具有可能不同類型的列,每一列可以是不同值類型(數值,字符串,布爾值),既有行索引也有列索引。我們可以把它看作為excel表格,或者SQL表,或Series對象的字典。

  2. 構建DateFrame的方法:

    字典類:數組、列表或元組構成的字典構造dataframe,Series構成的字典構造dataframe, 字典構成的字典構造dataframe

    列表類:2D ndarray 構造dataframe,字典構成的列表構造dataframe,Series構成的列表構造dataframe

  3. 查看數據 head()tail()

    pd5 = pd.DataFrame(np.arange(20).reshape(10,2))
    pd5
    	0	1
    0	0	1
    1	2	3
    2	4	5
    3	6	7
    4	8	9
    5	10	11
    6	12	13
    7	14	15
    8	16	17
    9	18	19
    
    # head()默認查看前5行,輸入參數N,就查看前N行
    pd5.head()
    	0	1
    0	0	1
    1	2	3
    2	4	5
    3	6	7
    4	8	9
    
    #tail()默認查看后5行,輸入參數N,就查看后N行
    pd5.tail()
    	0	1
    5	10	11
    6	12	13
    7	14	15
    8	16	17
    9	18	19
    
  4. .T

    #和Numpy一樣,進行轉置
    pd6 = pd.DataFrame(np.arange(4).reshape(2,2),index=['a','b'],columns=['A','B'])
    pd6
    	A	B
    a	0	1
    b	2	3
    
    pd6.T #行和列進行轉置
    	a	b
    A	0	2
    B	1	3
    
  5. 數組、列表或元組構成的字典構造dataframe

    #構造一個字典
    data = {'a':[1,2,3,4],
            'b':(5,6,7,8),
            'c':np.arange(9,13)}
    #構造dataframe
    frame = pd.DataFrame(data)
    frame
    	a	b	c
    0	1	5	9
    1	2	6	10
    2	3	7	11
    3	4	8	12	
    
    #index屬性查看行索引
    frame.index
    RangeIndex(start=0, stop=4, step=1)
    #columns屬性查看列索引
    frame.columns
    Index(['a', 'b', 'c'], dtype='object')
    #values屬性查看值
    frame.values
    array([[ 1,  5,  9],
           [ 2,  6, 10],
           [ 3,  7, 11],
           [ 4,  8, 12]], dtype=int64)
    # 每個序列是DateFrame的一列,所有的序列長度必須相等,columns為字典的key,index為默認的數字標簽,我們可以通過index屬性進行修改
    
    #指定index
    frame = pd.DataFrame(data,index=['A','B','C','D'])
    frame
    	a	b	c
    A	1	5	9
    B	2	6	10
    C	3	7	11
    D	4	8	12
    
    #指定columns,顯示所指定列的數據,並按指定的順序進行排序,當沒有數據中沒有該列('e'),那么就會用NaN來填充
    frame = pd.DataFrame(data,index=['A','B','C','D'],columns=['a','b','c','e'])
    frame
    	a	b	c	e
    A	1	5	9	NaN
    B	2	6	10	NaN
    C	3	7	11	NaN
    D	4	8	12	NaN
    
  6. 2D ndarray 構造dataframe

    #構造二維數組對象
    arr1 = np.arange(12).reshape(4,3)
    #構造dateframe
    frame1 = pd.DataFrame(arr1)
    frame1
    	0	1	2
    0	0	1	2
    1	3	4	5
    2	6	7	8
    3	9	10	11
    #我們通過二維數組對象創建dataframe,行索引和列索引都是可選參數,指定index和columns必須和原數組長度一致,默認0到N-1
    frame2 = pd.DataFrame(arr1,index=['a','b','c','d'],columns=['A','B','C'])
    frame2
    	A	B	C
    a	0	1	2
    b	3	4	5
    c	6	7	8
    d	9	10	11
    
  7. Series構成的字典構造dataframe

    pd1 = pd.DataFrame({'a':pd.Series(np.arange(3)),
                       'b':pd.Series(np.arange(3,5)), 
                       })
    pd1
    	a	b	
    0	0	4	
    1	1	5	
    2	2	NaN	
    
    #設置index,
    pd1 = pd.DataFrame({'a':pd.Series(np.arange(3),index=['a','b','c']),
                       'b':pd.Series(np.arange(3,5),index=['a','b']),
                       })
    pd1
    	a	b
    a	0	3.0
    b	1	4.0
    c	2	NaN
    #我們用Series構成的字典創建dataframe,指定索引我們需要在Series里面指定索引,index為Series的標簽,Series長度可以不一樣,會以NaN填充
    
  8. 字典構成的字典構造dataframe

    #字典嵌套
    data = {
        'a':{'apple':3.6,'banana':5.6},
        'b':{'apple':3,'banana':5},
        'c':{'apple':3.2}
    }
    #構造dataframe
    pd2 = pd.DataFrame(data3)
    pd2
    		a	b	c
    apple	3.6	3	3.2
    banana	5.6	5	NaN
    #內部字典是一列,內部字典的key是行索引index,外部字典的key是列索引columns,
    
  9. 字典構成的列表構造dataframe

    l1 = [{'apple':3.6,'banana':5.6},{'apple':3,'banana':5},{'apple':3.2}]
    pd3 = pd.DataFrame(l1)
    pd3
    	apple	banana
    0	3.6		5.6
    1	3.0		5.0
    2	3.2		NaN
    #列表中的每一個元素是一行,字典的key是列索引columns
    
    #指定行索引index,必須和數據長度一致
    pd3 = pd.DataFrame(l1,index=['a','b','c'])
    pd3
    	apple	banana
    a	3.6		5.6
    b	3.0		5.0
    c	3.2		NaN
    
  10. Series構成的列表構造dataframe

    l2 = [pd.Series(np.random.rand(3)),pd.Series(np.random.rand(2))]
    pd4=pd.DataFrame(l2)
    pd4
    	0			1			2
    0	0.482106	0.025374	0.020586
    1	0.912417	0.229153	NaN
    #列表中的每一個元素是一行
    #設置行索引index,和原數組長度一致
    pd4=pd.DataFrame(l2,index=['a','b'])
    pd4
    	0			1			2
    a	0.482106	0.025374	0.020586
    b	0.912417	0.229153	NaN
    #設置列索引columns,我們需要在series對象設置index
    l2 = [pd.Series(np.random.rand(3),index=['A','B','C']),pd.Series(np.random.rand(2),index=['A','B'])]
    
    pd4=pd.DataFrame(l2,index=['a','b'])
    pd4
    
    	  A			  B			 C
    a	0.999713	0.507880	0.091274
    b	0.798486	0.268391	NaN
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM