Pandas学习1 --- 数据载入
import numpy as np
import pandas as pd
数据加载
首先,我们需要将收集的数据加载到内存中,才能进行进一步的操作。pandas提供了非常多的读取数据的函数,分别应用在各种数据源环境中,我们常用的函数为:
- read_csv
- read_table
- read_sql
q
1.1 加载csv数据
- header 表标题,可以使用整形和或者整形列表来指定标题在哪一行,None是无标题,默认infer首行
- sep 控制数据之间的分隔符号。read_csv方法,默认为逗号(,)
- names 设置列标签(相当于df.columns)
- index_col 可以指定有唯一标记的列来充当行标签
- usecols 指定感兴趣的列
# 加载数据集, 返回DataFram类型
df = pd.read_csv('/home/geoffrey/文档/33.csv', header=0, sep=',', usecols=['v:0', 'Points:0', 'Points:1', 'Points:2'])
df.head(10)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| v:0 | Points:0 | Points:1 | Points:2 | |
|---|---|---|---|---|
| 0 | 2.57150 | 1.23150 | -0.86263 | -0.40724 |
| 1 | 2.08420 | 1.15670 | -0.90047 | -0.34635 |
| 2 | 1.27970 | 0.76719 | -0.93330 | -0.26176 |
| 3 | 0.71951 | 0.63454 | -0.91585 | -0.22918 |
| 4 | 1.63080 | 0.81560 | -0.93992 | -0.20332 |
| 5 | 3.36400 | 1.50590 | -0.98745 | -0.19570 |
| 6 | 2.27160 | 0.82635 | -0.89883 | -0.19312 |
| 7 | 2.64630 | 0.96451 | -0.85991 | -0.18457 |
| 8 | 0.91226 | 0.68853 | -0.83424 | -0.18203 |
| 9 | 4.55390 | 1.46730 | -0.82822 | -0.17043 |
1.2 加载数据库数据
pd.read_sql(sql语句, 连接对象)
import sqlite3
# 创建连接,创建数据库
con = sqlite3.connect('test.db')
# SQL语句
sql = 'create table person(id int primary key, name varchar(100))'
con.execute(sql)
# 插入数据
sql = 'insert into person(name) values("Geoffrey")'
con.execute(sql)
con.commit()
# 查看数据
sql = 'select * from person'
pd.read_sql(sql, con)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| id | name | |
|---|---|---|
| 0 | None | Geoffrey |
1.3 数据流处理
数据流.getvalue() # 注意,写入后指针在数据流的末尾,需要调整指针
from io import StringIO # 类文件对象(缓存区)
# 创建缓存区
sio = StringIO()
# 向缓存区写入数据
df.to_csv(sio)
# 读取数据
sio.getvalue()
',0,1,2\n0,1,2,3.0\n1,4,5,6.0\n2,7,8,\n'
# 调整指针到缓存区头部
sio.seek(0)
sio.read()
',0,1,2\n0,1,2,3.0\n1,4,5,6.0\n2,7,8,\n'
2. 写入数据
DataFrame与Series对象的to_csv方法:
该方法可以将数据写入:
- 文件中
- 数据流中
常用参数
- sep 指定分隔符
- header 是否写入标题行
- na_rep 空值的表示
- index 是否写入索引
- index_label 索引字段的名称
- columns 写入的字段
df = pd.DataFrame([
[1, 2, 3],
[4, 5, 6],
[7, 8, np.nan] # 含有
])
df
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 1 | 2 | 3.0 |
| 1 | 4 | 5 | 6.0 |
| 2 | 7 | 8 | NaN |
df.to_csv('test.csv', sep=',', header=True, index=True, na_rep='空', columns=[0, 2])
pd.read_csv('test.csv')
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| Unnamed: 0 | 0 | 2 | |
|---|---|---|---|
| 0 | 0 | 1 | 3.0 |
| 1 | 1 | 4 | 6.0 |
| 2 | 2 | 7 | 空 |
Pandas学习1 --- 数据载入的更多相关文章
- pandas学习(数据分组与分组运算、离散化处理、数据合并)
pandas学习(数据分组与分组运算.离散化处理.数据合并) 目录 数据分组与分组运算 离散化处理 数据合并 数据分组与分组运算 GroupBy技术:实现数据的分组,和分组运算,作用类似于数据透视表 ...
- pandas学习(创建多层索引、数据重塑与轴向旋转)
pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...
- pandas学习(常用数学统计方法总结、读取或保存数据、缺省值和异常值处理)
pandas学习(常用数学统计方法总结.读取或保存数据.缺省值和异常值处理) 目录 常用数学统计方法总结 读取或保存数据 缺省值和异常值处理 常用数学统计方法总结 count 计算非NA值的数量 de ...
- pandas学习(创建数据,基本操作)
pandas学习(一) Pandas基本数据结构 Series类型数据 Dataframe类型 基本操作 Pandas基本数据结构 两种常用数据结构: Series 一维数组,与Numpy中的一维ar ...
- pandas学习(四)--数据的归一化
欢迎加入python学习交流群 667279387 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学习(四)–数据 ...
- Pandas学习(一)——数据的导入
欢迎加入python学习交流群 667279387 学习笔记汇总 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学 ...
- 【转】Pandas学习笔记(二)选择数据
Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...
- 吴裕雄--天生自然python学习笔记:pandas模块导入数据
有时候,手工生成 Pandas 的 DataFrame 数据是件非常麻烦的事情,所以我们通 常会先把数据保存在 Excel 或数据库中,然后再把数据导入 Pandas . 另 一种情况是抓 取网页中成 ...
- 用scikit-learn和pandas学习线性回归
对于想深入了解线性回归的童鞋,这里给出一个完整的例子,详细学完这个例子,对用scikit-learn来运行线性回归,评估模型不会有什么问题了. 1. 获取数据,定义问题 没有数据,当然没法研究机器学习 ...
随机推荐
- OCP 相关课程列表
OCP 相关课程列表 第一天:Linux基础 和 Oracle 11 R2 数据库安装教程图解 1:< VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教 ...
- 用flask的扩展实现的简单的页面登录
from flask import Flask,render_template,request,redirect,session app = Flask(__name__,template_folde ...
- 【python】kafka在与celery和gevent连用时遇到的问题
前提:kafka有同步,多线程,gevent异步和rdkafka异步四种模式.但是在与celery和gevent连用的时候,有的模式会出错. 下面是我代码运行的结果. 结论:使用多线程方式! 使用同步 ...
- jsp 监听器
Servlet API提供了一系列的事件和事件监听接口. 上层的servlet/JSP应用能够通过调用这些API进行事件 驱动的开发.这里监听的所有事件都继承自 java.util.Event对象.监 ...
- kali linux 更新问题
1.使用一次更新和升级软件替换 apt-get install && apt -y full -upgrade 之后使用 reboot重启 系统,重启之后 再次使用命令 ap ...
- UEFI rootkit 工具LoJax可以感染电脑主板(mainboard)
1.UEFI(Unified Extensible Firmware Interface)统一扩展接口,UEFI rootkit是以在UEFI中植入rootkit ,18年9月份ESET首次公开了境外 ...
- Mesh无线网络的定义与WiFi的区别
Mesh无线网络的定义与WiFi的区别 无线Mesh网络(无线网状网络)也称为「多跳(multi-hop)」网络,它是一种与传统无线网络完全不同的新型无线网络技术.无线网状网是一种基于多跳路由,对等网 ...
- vue 的router的简易运用
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 005-2-Python文件操作
Python文件操作(file) 文件操作的步骤: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件(操作文件后记住关闭) 1.读写文件的基础语法: open() 将会返回一个 ...
- js中匿名函数和回调函数
匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...