Python数据分析之pandas读取数据
作者:小菜鸡也会有春天 时间:2023-06-17 12:53:59
一、三种数据文件的读取
二、csv、tsv、txt 文件读取
1)CSV文件读取:
语法格式:pandas.read_csv(文件路径)
CSV文件内容如下:
import pandas as pd
file_path = "e:\\pandas_study\\test.csv"
content = pd.read_csv(file_path)
content.head() # 默认返回前5行数据
content.head(3) # 返回前3行数据
content.shape # 返回一个元组(总行数,总列数),总行数不包括标题行
content.index # 返回索引,是一个可迭代的对象<class 'pandas.core.indexes.range.RangeIndex'>
content.column # 返回所有的列名 Index(['姓名', '年龄', '籍贯'], dtype='object')
content.dtypes # 返回的是每列的数据类型
姓名 object
年龄 int64
籍贯 object
dtype: object
2)CSV文件读取:
语法格式:pandas.read_csv(文件路径)
CSV文件内容如下:
import pandas as pd
file_path = "e:\\pandas_study\\test2.txt"
content = pd.read_csv(file_path,sep='\t',header = None ,names= ['name','age','adress'])
#参数说明:
# header = None 表示没有标题行
# sep='\t' 表示去除分割符中的空格
# names= ['name','age','adress'] ,列名依次自定义为'name','age','adress'
content.head() # 默认返回前5行数据
content.head(3) # 返回前3行数据
content.shape # 返回一个元组(总行数,总列数),总行数不包括标题行
content.index # 返回索引,是一个可迭代的对象<class 'pandas.core.indexes.range.RangeIndex'>
content.column # 返回所有的列名 Index(['姓名', '年龄', '籍贯'], dtype='object')
content.dtypes # 返回的是每列的数据类型
三、excel文件读取
import pandas as pd
file_path = "e:\\pandas_study\\test3.xlsx"
content = pd.read_excel(file_path)
content.head() # 默认返回前5行数据
content.head(3) # 返回前3行数据
content.shape # 返回一个元组(总行数,总列数),总行数不包括标题行
content.index # 返回索引,是一个可迭代的对象<class 'pandas.core.indexes.range.RangeIndex'>
content.column # 返回所有的列名 Index(['姓名', '年龄', '籍贯'], dtype='object')
content.dtypes # 返回的是每列的数据类型
姓名 object
年龄 int64
籍贯 object
dtype: object
四、数据库表格读取
语法: pandas.read_sql(sql语句,数据库连接对象)
数据对象的创建,可以根据pymysql,cx_oracle等模块连接mysql或者oracle。
来源:https://blog.csdn.net/weixin_50640351/article/details/117336508
标签:python,pandas
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一个基于flask的web应用诞生 用户注册功能开发(5)
2021-10-19 00:48:12
![](https://img.aspxhome.com/file/2023/0/133580_0s.jpg)
关于文件命名
2009-09-18 16:29:00
JavaScript循环遍历的24个方法,你都知道吗
2024-04-17 09:41:18
![](https://img.aspxhome.com/file/2023/6/136776_0s.png)
JS遮罩层效果 兼容ie firefox jQuery遮罩层
2024-02-24 23:04:24
Python实现画图软件功能方法详解
2023-08-29 06:35:49
![](https://img.aspxhome.com/file/2023/1/121371_0s.png)
基于JS实现html中placeholder属性提示文字效果示例
2023-09-07 22:50:58
![](https://img.aspxhome.com/file/2023/0/56130_0s.gif)
js 玩转正则表达式之语法高亮
2023-06-24 05:35:42
python 将md5转为16字节的方法
2023-09-17 18:46:37
vue 使用localstorage实现面包屑的操作
2024-05-10 14:19:40
![](https://img.aspxhome.com/file/2023/8/126408_0s.jpg)
python爬虫基础之urllib的使用
2022-02-10 19:01:18
python备份文件以及mysql数据库的脚本代码
2024-01-21 03:42:01
基于python 处理中文路径的终极解决方法
2023-07-21 00:47:09
浅谈webpack打包过程中因为图片的路径导致的问题
2024-04-28 09:42:38
![](https://img.aspxhome.com/file/2023/7/132817_0s.jpg)
ASP获取ACCESS数据库的表名以及表名对应的字段名和字段类型
2009-12-25 18:57:00
Django Celery异步任务队列的实现
2023-09-28 19:24:53
Python tabulate结合loguru打印出美观方便的日志记录
2021-02-23 02:39:45
简单谈谈GET和POST有什么区别
2022-06-06 04:53:41
django连接Mysql中已有数据库的方法详解
2024-01-23 09:00:59
![](https://img.aspxhome.com/file/2023/1/76841_0s.jpg)
Vue+Java+Base64实现条码解析的示例
2024-05-02 17:08:27
SQL触发器实例讲解
2024-01-24 13:46:55