Pandas入门:DataFrame的简单应用

    xiaoxiao2021-04-12  35

    Pandas

    Pandas是为了解决数据分析任务而创建的,纳入了大量库和一些标准的数据模型。在Python环境下我们经常会使用到Pandas去进行数据统计,而DataFrame简单的描述成数据框,是一种组织方式,我们看看如何使用DataFrame。

    DataFrame的使用

    1、首先引入pandas和numpy两个包,pandas依赖于numpy。

    import numpy as np import pandas as pd

    建立一个时间索引,索引即每一行数据的id,为唯一值。

    date=pd.date_range('20170401',periods=6) print(date)

    结果为:

    DatetimeIndex(['2017-04-01', '2017-04-02', '2017-04-03', '2017-04-04', '2017-04-05', '2017-04-06'], dtype='datetime64[ns]', freq='D')

    2、创建6*4的数据,random用于创建随机数,参数表示行数和列数,date是上面创建的索引列。

    df=pd.DataFrame(np.random.randn(6,4),index=date,columns=list('ABCD')) print(df)

    结果为:

    A B C D 2017-04-01 -1.174146 0.981851 0.054070 -0.304046 2017-04-02 -0.670757 -0.621248 -0.320604 -0.368905 2017-04-03 -0.008907 0.288560 -0.086716 1.601499 2017-04-04 -0.903410 -0.884107 -0.125471 -0.032931 2017-04-05 -0.589219 -0.561077 -0.994943 -0.001721 2017-04-06 1.503975 1.008683 0.631483 -0.713936

    3、可以使用dtypes查看各行的数据格式

    print(df.dtypes) A float64 B float64 C float64 D float64 dtype: object

    4、使用head查看前几行的数据(默认前5行)

    df.head() #查看前3行 df.head(3)

    使用tail查看后几行的数据,使用同head。 5、查看数据框的索引(index)、列名(columns)、数据值(values)、描述性(describe)

    df.index/df.columns/df.values/df.describe

    6、使用T进行转置数据,即行列转换。

    df.T 2017-04-01 2017-04-02 2017-04-03 2017-04-04 2017-04-05 2017-04-06 A -1.056570 -1.314146 -0.882650 -0.081214 -0.061534 -0.558024 B 0.221840 0.915488 -0.689384 -0.707948 0.791983 -0.300867 C -0.572469 -0.987231 -1.123992 0.332304 -0.476678 -0.409462 D 0.582291 0.012441 0.834482 -0.665694 0.765196 -0.020418

    7、使用sort进行数据排序

    df.sort(columns='A') A B C D 2017-04-05 -0.799361 -0.591627 -0.367348 0.268359 2017-04-04 -0.397003 -0.758888 -2.324696 1.310242 2017-04-01 -0.295905 -0.396839 -1.106580 1.013233 2017-04-03 -0.139464 -0.034839 -0.203179 -1.438849 2017-04-02 0.326524 0.922449 0.199526 0.217041 2017-04-06 0.376355 0.259185 -1.090140 0.060581
    转载请注明原文地址: https://ju.6miu.com/read-667819.html

    最新回复(0)