博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas的基本用法(一)——数据定义及使用
阅读量:3977 次
发布时间:2019-05-24

本文共 2594 字,大约阅读时间需要 8 分钟。

文章作者:Tyan

博客:  |   | 

本文主要是关于pandas的一些基本用法。

#!/usr/bin/env python# _*_ coding: utf-8 _*_import pandas as pdimport numpy as np# Test 1# 定义序列, pandas中的数据形式通常是float32或float64s = pd.Series([1, 3, 5, np.nan, 44,  1])print sprint s[0]print s[3]# Test 1 result0     1.01     3.02     5.03     NaN4    44.05     1.0dtype: float641.0nan# Test 2# 定义日期列表dates = pd.date_range('20170101', periods = 6)print datesprint dates[5]# Test 2 resultDatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',               '2017-01-05', '2017-01-06'],              dtype='datetime64[ns]', freq='D')2017-01-06 00:00:00# Test 3# DataFrame类似于numpy的array, 行索引为dates, 列索引为[a, b, c, d]df = pd.DataFrame(np.random.randn(6, 4), index = dates, columns = ['a', 'b', 'c', 'd'])print df# 不指定索引的DataFramedf = pd.DataFrame(np.arange(12).reshape(3, 4))print df# DataFrame的定义df = pd.DataFrame({
'A': 1., 'B': 'Foo', 'C': np.array([3] * 4)})print df# Test 3 result a b c d2017-01-01 1.104994 1.328379 0.410358 -1.6610592017-01-02 -0.642727 -0.152576 1.126191 -0.0053172017-01-03 -0.179257 0.160972 -0.824172 -0.1750272017-01-04 0.838328 -0.500909 0.714592 1.1448002017-01-05 0.803691 -3.979186 -1.037603 -0.7479432017-01-06 1.217289 -0.074413 0.504138 -0.077507 0 1 2 30 0 1 2 31 4 5 6 72 8 9 10 11 A B C0 1.0 Foo 31 1.0 Foo 32 1.0 Foo 33 1.0 Foo 3# Test 4# 查看DataFrame的数据类型df.dtypes# 查看DataFrame的索引df.index# 查看DataFrame的列索引df.columns# 查看DataFrame的值df.values# 查看DataFrame的描述df.describe()# DataFrame的转置df.T# DataFrame的index排序df.sort_index(axis = 1)# DataFrame的index排序, 逆序df.sort_index(axis = 1, ascending = False)# DataFrame按值排序df.sort_values(by = 'C')# Test 4 resultA float64B objectC int64dtype: objectRangeIndex(start=0, stop=4, step=1)Index([u'A', u'B', u'C'], dtype='object')array([[1.0, 'Foo', 3], [1.0, 'Foo', 3], [1.0, 'Foo', 3], [1.0, 'Foo', 3]], dtype=object) A Ccount 4.0 4.0mean 1.0 3.0std 0.0 0.0min 1.0 3.025% 1.0 3.050% 1.0 3.075% 1.0 3.0max 1.0 3.0 0 1 2 3A 1 1 1 1B Foo Foo Foo FooC 3 3 3 3 A B C0 1.0 3 Foo1 1.0 3 Foo2 1.0 3 Foo3 1.0 3 Foo C B A0 Foo 3 1.01 Foo 3 1.02 Foo 3 1.03 Foo 3 1.0 A B C0 1.0 3 Foo1 1.0 3 Foo2 1.0 3 Foo3 1.0 3 Foo

参考资料

转载地址:http://hcwui.baihongyu.com/

你可能感兴趣的文章
Flutter UI基础 - 侧拉抽屉菜单
查看>>
Flutter UI基础 - AppBar中标题文字如何居中
查看>>
Flutter UI基础 - Drawer 抽屉视图与自定义header
查看>>
Flutter UI基础 - GridView
查看>>
Flutter UI基础 - 使用InkWell给任意Widget添加点击事件
查看>>
OC WKWebView的使用
查看>>
Flutter UI基础 - Image.asset 图片铺满布局
查看>>
Flutter UI基础 - Row、Column详解
查看>>
Flutter UI基础 - 添加背景图片
查看>>
Flutter UI基础 - 布局之Row/Column/Stack
查看>>
Flutter UI基础 - 层叠布局Stack的使用
查看>>
Go - 解决 go get 超时问题
查看>>
SQL - SQL Server 之遍历数据集合的几种方法
查看>>
SQL - SQL Server 之处理JSON数据
查看>>
SQL - SQL Server 之WHILE循环的坑
查看>>
SQL - SQL Server 性能优化之SQL语句总结
查看>>
Docker - docker-compose常用命令
查看>>
SQL - SQL Server判断字符串中是否有中文
查看>>
SQL - SQL Server查询近7天的连续日期
查看>>
SQL - SQL Server中如何取年、月、日 -DATEPART函数
查看>>