# -*- encoding:utf-8 -*-
# Copyright (c) 2015 Shiye Inc.
# All rights reserved.
#
# Author: ldq <liangduanqi@shiyejinrong.com>
# Date: 2019/2/12 10:07 import numpy as np
import pandas as pd dates = pd.date_range("", periods=5)
'''
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06'],
dtype='datetime64[ns]', freq='D')
'''
df = pd.DataFrame(np.random.randn(5, 4), index=dates,
columns=["a", "b", "c", "d"])
'''
a b c d
2019-01-01 -0.406321 -0.518128 -0.151546 1.438366
2019-01-02 -0.738235 0.400646 1.337277 1.393154
2019-01-03 1.646115 -0.073540 0.644506 0.987226
2019-01-04 -1.270745 -1.333457 -1.571356 -0.051486
2019-01-05 -0.075171 2.424032 -0.274433 1.205959
'''
df1 = pd.DataFrame(np.arange(12).reshape(3, 4))
'''
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
'''
data2 = {
"a": 1,
"b": pd.Timestamp(""),
"c": pd.Series(1, index=range(4), dtype=np.float64),
"d": np.array([3] * 4, dtype=np.int32),
"e": pd.Categorical(["test", "train", "test", "train"]),
"f": "foo",
"g": pd.date_range("",periods=4),
}
df2 = pd.DataFrame(data2)
'''
a b c d e f g
0 1 2019-01-01 1.0 3 test foo 2002-02-05
1 1 2019-01-01 1.0 3 train foo 2002-02-06
2 1 2019-01-01 1.0 3 test foo 2002-02-07
3 1 2019-01-01 1.0 3 train foo 2002-02-08
'''
columns1 = df2.columns
'''
所有列
Index(['a', 'b', 'c', 'd', 'e', 'f', 'g'], dtype='object')
'''
index1 = df2.index
'''
RangeIndex(start=0, stop=4, step=1)
'''
values1 = df2.values
'''
[[1 Timestamp('2019-01-01 00:00:00') 1.0 3 'test' 'foo'
Timestamp('2002-02-05 00:00:00')]
[1 Timestamp('2019-01-01 00:00:00') 1.0 3 'train' 'foo'
Timestamp('2002-02-06 00:00:00')]
[1 Timestamp('2019-01-01 00:00:00') 1.0 3 'test' 'foo'
Timestamp('2002-02-07 00:00:00')]
[1 Timestamp('2019-01-01 00:00:00') 1.0 3 'train' 'foo'
Timestamp('2002-02-08 00:00:00')]]
'''
describe1 = df2.describe()
'''
数据简单统计
a c d
count 4.0 4.0 4.0
mean 1.0 1.0 3.0
std 0.0 0.0 0.0
min 1.0 1.0 3.0
25% 1.0 1.0 3.0
50% 1.0 1.0 3.0
75% 1.0 1.0 3.0
max 1.0 1.0 3.0
'''
transpose1 = df2.T
'''
数据翻转
0 ... 3
a 1 ... 1
b 2019-01-01 00:00:00 ... 2019-01-01 00:00:00
c 1 ... 1
d 3 ... 3
e test ... train
f foo ... foo
g 2002-02-05 00:00:00 ... 2002-02-08 00:00:00 [7 rows x 4 columns]
'''
df2_sort_index = df2.sort_index(axis=0, ascending=False)
'''
对行和列的索引进行排序
a b c d e f g
3 1 2019-01-01 1.0 3 train foo 2002-02-08
2 1 2019-01-01 1.0 3 test foo 2002-02-07
1 1 2019-01-01 1.0 3 train foo 2002-02-06
0 1 2019-01-01 1.0 3 test foo 2002-02-05
'''
df2_sort_values = df2.sort_values(by='g', ascending=False)
'''
根据值排序
a b c d e f g
3 1 2019-01-01 1.0 3 train foo 2002-02-08
2 1 2019-01-01 1.0 3 test foo 2002-02-07
1 1 2019-01-01 1.0 3 train foo 2002-02-06
0 1 2019-01-01 1.0 3 test foo 2002-02-05
'''

Pandas 1 表格数据类型DataFrame的更多相关文章

  1. pandas中的数据结构-DataFrame

    pandas中的数据结构-DataFrame DataFrame是什么? 表格型的数据结构 DataFrame 是一个表格型的数据类型,每列值类型可以不同 DataFrame 既有行索引.也有列索引 ...

  2. pandas库的数据类型运算

    pandas库的数据类型运算 算数运算法则 根据行列索引,补齐运算(不同索引不运算,行列索引相同才运算),默认产生浮点数 补齐时默认填充NaN空值 二维和一维,一维和0维之间采用广播运算(低维元素与每 ...

  3. 数据类型-DataFrame

    数据类型-DataFrame DataFrame是由多个Series数据列组成的表格数据类型,每行Series值都增加了一个共用的索引 既有行索引,又有列索引 行索引,表明不同行,横向索引,叫inde ...

  4. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  5. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  6. pandas向表格中循环写入数据

    pandas向表格中循环写入多行数据 import pandas as pd def list_topic(total_num, str1): """ 生成多个主题 :p ...

  7. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  8. python简单爬虫 使用pandas解析表格,不规则表格

    url = http://www.hnu.edu.cn/xyxk/xkzy/zylb.htm 部分表格如图: 部分html代码: <table class="MsoNormalTabl ...

  9. pandas使用drop_duplicates去除DataFrame重复项

    DataFrame中存在重复的行或者几行中某几列的值重复,这时候需要去掉重复行,示例如下: data.drop_duplicates(subset=['A','B'],keep='first',inp ...

随机推荐

  1. react native 中时间选择插件

    npm install react-native-datepicker --save import DatePicker from 'react-native-datepicker'; <Vie ...

  2. 域 搭建OU 组织单元

    以这个界面开始操作: 在 baidu.com 右键---新建----组织单位----北京分公司 在 baidu.com 右键---新建----组织单位----北京分公司 在北京分公司 和南京分公司下面 ...

  3. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

  4. Linux命令--tree

    目录 tree 最常用 带颜色显示2级目录 排除显示某个目录 tree tree -C :颜色显示 tree -f : 显示文件全路径 tree -L 2 :只显示2层 tree -P *.pl :只 ...

  5. QuerySet

    ################################################################## # PUBLIC METHODS THAT ALTER ATTRI ...

  6. python序列化与反序列化(json与pickle)

    在python中,序列化可以理解为将python中对象的编码格式转换为json(pickle)格式的字符串,而反序列化可以 理解为将json(pickle)格式的字符串转换为python中对象的编码格 ...

  7. java界面--WePush-master 项目跑起来 -碰到的问题

    仅能在IntelliJ里面打开GUI Form文件来进行界面的拖拽设计--eclipse中也可以了 当eclipse 打开.form文件时,提示有.对应的编辑工具可以打开它,请到市场去安装 但是:本人 ...

  8. redis批量灌库

    需求:将批量数据灌入redis中 如果通过代码形式将数据灌入redis中,效率比较低,以下将根据redis的特性进行快速的批量灌库 环境:centos7 将数据整理成规定格式的文件,比如: SET k ...

  9. 在Linux搭建Git服务器

    搭建Git服务器 https://www.cnblogs.com/dee0912/p/5815267.html Git客户端的安装 https://www.cnblogs.com/xuwenjin/p ...

  10. Beta答辩总结

    组员名单 短学号 姓名 备注 409 后敬甲 组长 301 蔡文斌 315 黄靖茹 423 刘浩 317 黄泽 328 卢泽明 617 葛亮 344 张杰 348 朱跃安 链接汇总 组长博客:后敬甲 ...