pandas入门——数据的创建与基本操作

来源:互联网 发布:js引用本地json文件 编辑:程序博客网 时间:2024/06/07 13:30

数据的创建与基本操作

  • 建一个dataframe 使用时间序列为行索引,使用abcdef为列索引
# 导入numpy包以np的形式;导入pandas包以pd的形式import numpy as npimport pandas as pd# 创建一个时间序列dates = pd.date_range("20170813",periods=6)# 创建一个dataframe 使用时间序列为行索引,使用abcdef为列索引df = pd.DataFrame(data=np.random.randint(3, 9,size=(6,6)),index=dates,columns=list(["a","b","c","d","e","f"]))print(df)
            a   b   c   d   e   f2017-08-13  5   3   7   8   8   62017-08-14  3   7   5   3   3   72017-08-15  5   3   8   8   8   52017-08-16  4   3   8   8   3   82017-08-17  6   5   5   4   4   82017-08-18  6   6   4   3   7   5
  • 获取每一column的数据类型
print(df.dtypes)
a    int32b    int32c    int32d    int32e    int32f    int32dtype: object
  • 获取数据的index
print(df.index)
DatetimeIndex(['2017-08-13', '2017-08-14', '2017-08-15', '2017-08-16',               '2017-08-17', '2017-08-18'],              dtype='datetime64[ns]', freq='D')
  • 获取数据的columns
print(df.columns)
Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')
  • 获取数据的所有values
print(df.values)
array([[5, 3, 7, 8, 8, 6],       [3, 7, 5, 3, 3, 7],       [5, 3, 8, 8, 8, 5],       [4, 3, 8, 8, 3, 8],       [6, 5, 5, 4, 4, 8],       [6, 6, 4, 3, 7, 5]])
  • 获取数据的描述信息
print(df.describe())
a   b   c   d   e   fcount   6.000000    6.000000    6.000000    6.000000    6.000000    6.000000mean    4.833333    4.500000    6.166667    5.666667    5.500000    6.500000std 1.169045    1.760682    1.722401    2.581989    2.428992    1.378405min 3.000000    3.000000    4.000000    3.000000    3.000000    5.00000025% 4.250000    3.000000    5.000000    3.250000    3.250000    5.25000050% 5.000000    4.000000    6.000000    6.000000    5.500000    6.50000075% 5.750000    5.750000    7.750000    8.000000    7.750000    7.750000max 6.000000    7.000000    8.000000    8.000000    8.000000    8.000000
  • 对行上的索引进行逆向排序
print(df.sort_index(axis=1,ascending=False))
            f   e   d   c   b   a2017-08-13  6   8   8   7   3   52017-08-14  7   3   3   5   7   32017-08-15  5   8   8   8   3   52017-08-16  8   3   8   8   3   42017-08-17  8   4   4   5   5   62017-08-18  5   7   3   4   6   6
  • 对列上的索引进行逆向排序
print(df.sort_index(axis=0,ascending=False))
            a   b   c   d   e   f2017-08-18  6   6   4   3   7   52017-08-17  6   5   5   4   4   82017-08-16  4   3   8   8   3   82017-08-15  5   3   8   8   8   52017-08-14  3   7   5   3   3   72017-08-13  5   3   7   8   8   6
  • 对数据进行排序 指定在行上进行排序 并以倒序的形式
print(df.sort_values(by="2017-08-13",axis=1,ascending=False))
            d   e   c   f   a   b2017-08-13  8   8   7   6   5   32017-08-14  3   3   5   7   3   72017-08-15  8   8   8   5   5   32017-08-16  8   3   8   8   4   32017-08-17  4   4   5   8   6   52017-08-18  3   7   4   5   6   6
  • 对数据进行排序 指定在列上进行排序 并以倒序的形式
print(df.sort_values(by="e",axis=0,ascending=False))
            a   b   c   d   e   f2017-08-13  5   3   7   8   8   62017-08-15  5   3   8   8   8   52017-08-18  6   6   4   3   7   52017-08-17  6   5   5   4   4   82017-08-14  3   7   5   3   3   72017-08-16  4   3   8   8   3   8