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

数据的创建与基本操作

  • 建一个dataframe 使用时间序列为行索引,使用abcdef为列索引
# 导入numpy包以np的形式;导入pandas包以pd的形式
import numpy as np
import 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   f
2017-08-13  5   3   7   8   8   6
2017-08-14  3   7   5   3   3   7
2017-08-15  5   3   8   8   8   5
2017-08-16  4   3   8   8   3   8
2017-08-17  6   5   5   4   4   8
2017-08-18  6   6   4   3   7   5
  • 获取每一column的数据类型
print(df.dtypes)
a    int32
b    int32
c    int32
d    int32
e    int32
f    int32
dtype: 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   f
count   6.000000    6.000000    6.000000    6.000000    6.000000    6.000000
mean    4.833333    4.500000    6.166667    5.666667    5.500000    6.500000
std 1.169045    1.760682    1.722401    2.581989    2.428992    1.378405
min 3.000000    3.000000    4.000000    3.000000    3.000000    5.000000
25% 4.250000    3.000000    5.000000    3.250000    3.250000    5.250000
50% 5.000000    4.000000    6.000000    6.000000    5.500000    6.500000
75% 5.750000    5.750000    7.750000    8.000000    7.750000    7.750000
max 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   a
2017-08-13  6   8   8   7   3   5
2017-08-14  7   3   3   5   7   3
2017-08-15  5   8   8   8   3   5
2017-08-16  8   3   8   8   3   4
2017-08-17  8   4   4   5   5   6
2017-08-18  5   7   3   4   6   6
  • 对列上的索引进行逆向排序
print(df.sort_index(axis=0,ascending=False))

            a   b   c   d   e   f
2017-08-18  6   6   4   3   7   5
2017-08-17  6   5   5   4   4   8
2017-08-16  4   3   8   8   3   8
2017-08-15  5   3   8   8   8   5
2017-08-14  3   7   5   3   3   7
2017-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   b
2017-08-13  8   8   7   6   5   3
2017-08-14  3   3   5   7   3   7
2017-08-15  8   8   8   5   5   3
2017-08-16  8   3   8   8   4   3
2017-08-17  4   4   5   8   6   5
2017-08-18  3   7   4   5   6   6
  • 对数据进行排序 指定在列上进行排序 并以倒序的形式
print(df.sort_values(by="e",axis=0,ascending=False))
            a   b   c   d   e   f
2017-08-13  5   3   7   8   8   6
2017-08-15  5   3   8   8   8   5
2017-08-18  6   6   4   3   7   5
2017-08-17  6   5   5   4   4   8
2017-08-14  3   7   5   3   3   7
2017-08-16  4   3   8   8   3   8
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值