# -*- 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. 一、C语言调试—— gdb 的使用

    1.1 gdb 调试工具常用命令 list:展开调试的源代码,缩写 l: break:设置断点,缩写为 b: info break:查看断点信息,缩写为 i b delete:删除断点 print:打 ...

  2. Python学习第二节——基础知识

    # !/usr/bin/edv python    脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它.# -*- coding:UTF-8 -*-    标明编码注释 ...

  3. mysql Using filesort 索引不可用问题

        今天上班发现线上机器CPU告警,看了一下发现是mysqld一直占用CPU处于满负荷状态,show processlist;一下,发现很多查询在排序状态,随便拿了一条sql explain看了一 ...

  4. 第30月第13天 supportedInterfaceOrientationsForWindow旋转

    1. 对于做视频横屏播放的情况下:做旋转有3种方法. 第一种:就是网上说的用旋转矩阵方法CGAffineTransformMakeRotation来做,直接旋转某个view,之后setFrame,至于 ...

  5. Python-Struct

    从一个例子开始: >>> from struct import * >>> pack('hhl',1655, 255, 370) b'w\x06\xff\x00r\ ...

  6. temp 和 tmp 文件

    TMP和TEMP文件是各种软件或系统产生的临时文件,也就是常说的垃圾文件.Windows产生的临时文件,本质上和虚拟内存没什么两样,只不过临时文件比虚拟内存更具有针对性,单独为某个程序服务而已.而它的 ...

  7. Docker-----常见问题

    docker中删除dead状态的容器 其现象如下:docker ps -a docker rm ytn删除时报错如下:解决方法: 先查出其进程,kill掉,在进行删除 查 ,复制上图白底部分,用以下命 ...

  8. 管理外部表(External Tables)

    Oracle数据库允许对外部表中的数据进行只读访问.外部表定义为不驻留在数据库中的表,并且可以是为其提供访问驱动程序的任何格式.通过为数据库提供描述外部表的元数据,数据库能够公开外部表中的数据,就好像 ...

  9. Debian 无线网络切换问题解决方案

    sudo gedit /etc/NetworkManager/NetworkManager.conf 2: 添加 [device] wifi.scan-rand-mac-address=no 3:重启 ...

  10. c#--Redis帮助类

    最近一直在忙公司的一下项目,也没有太多时间写,所以就分享出所用redis帮助类 using Newtonsoft.Json; using StackExchange.Redis; using Syst ...