pandas入门——数据的创建与基本操作
2017-08-15 18:11
363 查看
数据的创建与基本操作
建一个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
相关文章推荐
- MySql入门之一:DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- 学习opencv第三章 基本数据类型 ,矩阵创建,操作,访问
- 利用Pandas进行数据分析(2)——pandas数据基本操作
- Python数据分析库pandas基本操作
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- linux学习入门3——linux文件基本操作(linuxcast.net)(复制、移动、重命名、创建、删除文件及文件夹)
- 利用 Python 进行数据分析(八)pandas 基本操作(Series 和 DataFrame)
- 大数据入门第十一天——hive详解(二)基本操作与分区分桶
- 【pandas】[2] DataFrame 基础,创建DataFrame和增删改查基本操作(1)
- Pandas 数据框增、删、改、查、去重、抽样基本操作
- Pandas 数据框增、删、改、查、去重、抽样基本操作
- Redis从入门到放弃 之 常用命令和基本数据类型操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 用python做数据分析4|pandas库介绍之DataFrame基本操作 by 是蓝先生
- 第2章_基本数据类型和基本操作_编程练习Exercise2.7通过创建窗口进行大小写的切换
- 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作
- MySql之DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- 数据分析之Pandas(一):Series、DataFrame基本操作及索引对象
- (大数据工程师学习路径)第一步 Linux 基础入门----目录结构及文件基本操作
- Python 数据处理库 pandas 入门教程基本操作