Pandas 1 表格数据类型DataFrame
# -*- 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的更多相关文章
- pandas中的数据结构-DataFrame
pandas中的数据结构-DataFrame DataFrame是什么? 表格型的数据结构 DataFrame 是一个表格型的数据类型,每列值类型可以不同 DataFrame 既有行索引.也有列索引 ...
- pandas库的数据类型运算
pandas库的数据类型运算 算数运算法则 根据行列索引,补齐运算(不同索引不运算,行列索引相同才运算),默认产生浮点数 补齐时默认填充NaN空值 二维和一维,一维和0维之间采用广播运算(低维元素与每 ...
- 数据类型-DataFrame
数据类型-DataFrame DataFrame是由多个Series数据列组成的表格数据类型,每行Series值都增加了一个共用的索引 既有行索引,又有列索引 行索引,表明不同行,横向索引,叫inde ...
- Python之Pandas中Series、DataFrame
Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...
- Python之Pandas中Series、DataFrame实践
Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...
- pandas向表格中循环写入数据
pandas向表格中循环写入多行数据 import pandas as pd def list_topic(total_num, str1): """ 生成多个主题 :p ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- python简单爬虫 使用pandas解析表格,不规则表格
url = http://www.hnu.edu.cn/xyxk/xkzy/zylb.htm 部分表格如图: 部分html代码: <table class="MsoNormalTabl ...
- pandas使用drop_duplicates去除DataFrame重复项
DataFrame中存在重复的行或者几行中某几列的值重复,这时候需要去掉重复行,示例如下: data.drop_duplicates(subset=['A','B'],keep='first',inp ...
随机推荐
- WebGL&Three.js工作原理
一.我们讲什么? 我们讲两个东西:1.WebGL背后的工作原理是什么?2.以Three.js为例,讲述框架在背后扮演什么样的角色? 二.我们为什么要了解原理? 我们假定你对WebGL已经有一定了解,或 ...
- XSS绕过小结
0x00前言 我们友情进行XSS检查,偶然跳出个小弹窗,其中我们总结了一些平时可能用到的XSS插入方式,方便我们以后进行快速检查,也提供了一定的思路,其中XSS有反射.存储.DOM这三类,至于具体每个 ...
- Hexo博客部署-使用github作为保存中转仓库
本篇是在VPS上搭建Hexo静态博客的第一篇博文,写本篇的目的一是纪念一下,二是作为一个部署文档保留. 博客地址 相关描述 VPS环境是在搬瓦工上安装的centos6(x86),1核,512MB,10 ...
- git体验
(1)git初始化配置#配置用户名git config --global user.name "azcode"#配置邮箱git config --global user.email ...
- JS 循环定时的一些思考
网上也有例子, function doSetTimeout(i) { setTimeout(function() { console.log(i); }, 1000); } for (var i = ...
- ajax上传文件显示进度
下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...
- [转]Jupyter默认目录和默认浏览器修改
转摘于:https://blog.csdn.net/caterfreelyf/article/details/79774311 1.打开cmd,首先进入到Jupyter的安装目录,我的是在D:\Pyt ...
- Android应用市场的帮助类
写了一个Android应用市场的帮助类,如下: public class MarketUtils { public static final String MARKET_DATA = "ma ...
- js数据结构与算法——队列
<script> //创建一个队列 function Queue(){ let items = []; //向队尾添加一个新的项 this.enqueue = function(eleme ...
- IntelliJ IDEA重启Tomcat