zoukankan      html  css  js  c++  java
  • pandas之DataFrame数据分析

    2.DATAFRAME

    dataframe是我们最常使用的数据结构,它含有一组有序的列,每一列可以是不同的类型

    import numpy as np
    import pandas as pd
    pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
    """
    data: numpy ndarray(结构化或同类),dict或DataFrame,Dict可以包含Series,数组,常量或类似列表的对象
    index: dataframe的索引,如果没有自定义,则默认为RangeIndex(0,1,2,...,n)
    columns:dataframe的列标签,如果没有自定义,则默认为RangeIndex(0,1,2,...,n)
    dtype:默认None,要强制的数据类型。 只允许一个dtype
    copy:boolean,默认为False
    """
    

    创建DataFrame

    df1 = pd.DataFrame(np.array([[10,20,25],[30,40,45]]))
    print(df1)
    """
        0   1   2
    0  10  20  25
    1  30  40  45
    """
    df2 = pd.DataFrame([np.arange(1,8),np.arange(11,18)])
    print(df2)
    """
        0   1   2   3   4   5   6
    0   1   2   3   4   5   6   7
    1  11  12  13  14  15  16  17
    """
    df1.df1.shape
    """
    两行三列
    (2, 3)
    """
    df3 = pd.DataFrame(np.array([[10,20,25],[30,40,45]]),index=("a","b"),columns=("c1","c2","c3"))
    df3
    """
    index: 行索引
    columns:列标签
    	c1	c2	c3
    a	10	20	25
    b	30	40	45
    """
    df3.index
    """
    取行索引
    Index(['a', 'b'], dtype='object')
    """
    df3.columns[0]
    """
    取列标签第一个
    'c1'
    """
    df3.columns = ["h1","h2","h3"]
    df3
    """
    修改列标签
    	h1	h2	h3
    a	10	20	25
    b	30	40	45
    """
    df4 = pd.DataFrame({'a':[1,2,3],'b':5})
    df4
    """
    	a	b
    0	1	5
    1	2	5
    2	3	5
    """
    l1 = pd.Series(np.arange(1,9,2))
    l2 = pd.Series(np.arange(2,10,2))
    l1
    """
    1-9以2为间隔的Series数据
    0    1
    1    3
    2    5
    3    7
    """
    df5 = pd.DataFrame({"a1":l1,"a2":l2})
    df5
    """
    	a1	a2
    0	1	2
    1	3	4
    2	5	6
    3	7	8
    """
    

    CSV中读取数据

    eu1 = pd.read_csv(r"C:UsersAdministratorDesktopcsv_file.csv")
    eu1
    """
    文件数据
    	a	b	c	d	e	f	g	h
    0	1	11	112	1123	11234	112345	1123456	11234567
    1	2	20	200	242	4453	5434	7867	3768324
    2	2	30	300	8348	36738	786	45723	678
    3	4	40	400	76867	76786	78678	3683	78
    4	5	50	500	5543	42345	368736	38533	38763
    5	6	60	600	6786	5675	767	464565	457
    6	7	70	700	37417	3	38737	543737	37632
    7	8	80	800	678	3453	7867	34537	38763
    8	9	90	900	434537	7378	8	2137	378
    9	10	100	1000	7378	7843	7678	78	3534
    """
    eu1.head(3)
    """
    head(3)显示前3行数据,默认前5行
    	a	b	c	d	e	f	g	h
    0	1	11	112	1123	11234	112345	1123456	11234567
    1	2	20	200	242	4453	5434	7867	3768324
    2	2	30	300	8348	36738	786	45723	678
    """
    eu12.tail(2)
    """
    tail(2)显示后两行数据,默认后5行
    	a	b	c	d	e      f     g	    h
    8	9	90	900     434537	7378  8	     2137   378
    9	10	100	1000    7378	7843  7678   78     3534
    """
    len(eu1)
    """
    eu1的数据条数(行)
    10
    """
    eu1.shape
    """
    10行8列
    (10,8)
    """
    eu1.shape[0]
    """
    10行
    10
    """
    eu1.shape[1]
    """
    8列
    8
    """
    eu1.index
    """
    起始:0, 结束:10,步长:1
    RangeIndex(start=0, stop=10, step=1)
    """
    

    DataFrame操作数据的行/列

    1.使用 [ ] 运算符进行切片,按照索引能够实现行选择或列选择或区块选择
    # 行选择
    eu1[1:4]
    """
    1-3行的数据
    	a	b	c	d	e	f	g	h
    1	2	20	200	242	4453	5434	7867	3768324
    2	2	30	300	8348	36738	786	45723	678
    3	4	40	400	76867	76786	78678	3683	78
    """
    eu1.index.get_loc(0)
    """
    eu1.index.get_loc('行标签') 0标签的索引
    0
    """
    

    列选择
    多列选择,不能像行选择时一样使用num1:num2这样的方法来选择
    eu1['b']
    type(eu1['b'])
    """
    标签为bt的一列数据,Series类型
    0 11
    1 20
    2 30
    3 40
    4 50
    5 60
    6 70
    7 80
    8 90
    9 100
    Name: b, dtype: int64

    pandas.core.series.Series
    """
    eu1[['b']]
    """
    标签为b的一列数据,DataFrame类型
    b
    0 11
    1 20
    2 30
    3 40
    4 50
    5 60
    6 70
    7 80
    8 90
    9 100

    pandas.core.frame.DataFrame
    """
    eu1[['a','b']]
    """
    eu12[['开始列','结束列']]
    a b
    0 1 11
    1 2 20
    2 2 30
    3 4 40
    4 5 50
    5 6 60
    6 7 70
    7 8 80
    8 9 90
    9 10 100
    """
    eu1.columns.get_loc('a')
    """
    eu1.columns.get_loc('a') a列的索引
    0
    """
    区块选择
    eu1[2:4][['a','b']]
    """
    获取第2行第4行中,'a'和'b'列的值
    a b
    2 2 30
    3 4 40
    """

    2.使用 loc 按照索引(index)的值来进行行列选择
    # 行选择
    eu1.loc[2]
    type(eu1.loc[2])
    """
    第2行的数据
    a        2
    b       30
    c      300
    d     8348
    e    36738
    f      786
    g    45723
    h      678
    Name: 2, dtype: int64
    

    pandas.core.series.Series
    """
    eu1.loc[[2]]
    type(eu1.loc[[2]])
    """
    第2行的数据
    a b c d e f g h
    2 2 30 300 8348 36738 786 45723 678

    pandas.core.frame.DataFrame
    """
    eu1.loc[1:2]
    """
    1-2行的数据
    a b c d e f g h
    1 2 20 200 242 4453 5434 7867 3768324
    2 2 30 300 8348 36738 786 45723 678
    """
    列选择
    eu1.loc[1:2,['a','b']]
    """
    数据基础上去a,b列
    a b
    1 2 20
    2 2 30
    """
    只能去连续的块选
    eu1.loc[1:4,['a','b','c']]
    """
    1-4行,'a','b','c'列
    """

    3.使用 iloc 按照索引 (index) 的位置来进行选取

    iloc全是以0开头的行号和列号

    # 行选择
    eu1.iloc[[2]]
    """
    2行数据
    	a	b	c	d	 e	f	g	h
    2	2	30	300	8348 	36738	786	45723 	678
    """
    eu1.iloc[0:2]
    """
    0-1行的数据
    	a	b	c	d	e	f	g	h
    0	1	11	112	1123	11234	112345	1123456	11234567
    1	2	20	200	242	4453	5434	7867	3768324	
    """
    列选择
    eu1.iloc[0:2,[1,2]]
    """
    0-1行中,列索引为1,2的数据
    	b	c
    0	11	112
    1	20	200
    """
    块选择
    eu1.iloc[[1,2,4],[1,3]]
    """
    1,2,4行; 1,3列
    	b	d
    1	20	242
    2	30	8348
    4	50	5543
    """
    
    4.使用 .at[ ] 和 .iat[ ] 按标签或位置进行标量查找(获取某一行列的值)

    at的使用方法与loc类似,但是比loc有更快的访问数据的速度,但只能访问单个元素,不能访问多个元素

    iat对于iloc的关系就像at对于loc的关系,是一种更快的基于索引位置的选择方法,同at一样只能访问单个元素

    eu1.at[2,'b']
    """
    获取第2行b列的元素
    20
    """
    eu1.iat[2,1]
    """
    获取第2行1列的元素
    30
    """
    
    5.使用 .ix[]

    以上说过的几种方法都要求查询的秩在索引中,或者位置不超过长度范围,而ix允许你得到不在DataFrame索引中的数据。

    eu1.ix[1:12]
    """
    数据只到9行
    	a	b	c	d	e	f	g	h
    1	2	20	200	242	4453	5434	7867	3768324
    2	2	30	300	8348	36738	786	45723	678
    3	4	40	400	76867	76786	78678	3683	78
    4	5	50	500	5543	42345	368736	38533	38763
    5	6	60	600	6786	5675	767	464565	457
    6	7	70	700	37417	3	38737	543737	37632
    7	8	80	800	678	3453	7867	34537	38763
    8	9	90	900	434537	7378	8	2137	378
    9	10	100	1000	7378	7843	7678	78	3534
    """
    eu1.ix[[9,11]]
    """
    	a	b	c	d	e	f	g	h
    9	10.0	100.0	1000.0	7378.0	7843.0	7678.0	78.0	3534.0
    11	NaN		NaN		NaN		NaN		NaN		NaN		NaN		NaN	
    """
    

    a

    参考连接--https://lupython.gitee.io/2017/04/07/pandas的介绍/

  • 相关阅读:
    noi.ac 集合
    NOI2019 SX 模拟赛 no.5
    带花树草解
    UR#13 SRAND
    【51nod1847】 奇怪的数学题
    ●POJ 3237 Tree
    ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测
    ●BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
    ●POJ 2983 Is the Information Reliable?
    ●POJ 3378 Crazy Thairs
  • 原文地址:https://www.cnblogs.com/bbiu/p/11769497.html
Copyright © 2011-2022 走看看