当前位置:网站首页>数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
2022-07-26 10:27:00 【猿知】
一、数据加载
本次主要以实战的方式了解数据分析的流程和熟悉数据分析python的基本操作,完成kaggle上泰坦尼克的任务,实战数据分析全流程。可以参考的图书《Python for Data Analysis》。
数据集:https://www.kaggle.com/c/titanic/overview
(1)载入数据
这里主要使用numpy
和pandas
库
import numpy as np
import pandas as pd
载入数据时可以选择使用相对路径
或者绝对路径
载入数据
df = pd.read_csv('train.csv')
df.head(3)
这里也可以使用read_table()
读取数据,不过read_table是按照\t分割,而read_csv()
是按照逗号,这里如果使用了table可以加入参数 sep = ','实现和read_csv一样的效果。
(2)逐块读取
日常数据分析工作中,难免碰到数据量特别大的情况,动不动就2、3千万行,如果直接读进 Python 内存中,且不说内存够不够,读取的时间和后续的处理操作都很费劲。
Pandas 的 read_csv 函数提供2个参数:chunksize
、iterator
,可实现按行多次读取文件,避免内存不足情况。
使用语法为:
- iterator : boolean, default False
返回一个TextFileReader 对象,以便逐块处理文件。 - chunksize : int, default None
文件块的大小, See IO Tools docs for more informationon iterator and chunksize.
chunker = pd.read_csv('train.csv',chunksize = 1000)
<pandas.io.parsers.TextFileReader at 0x2087ab29040>
pandas.read_csv 参数 chunksize
通过指定一个分块大小(每次读取多少行)来读取大数据文件,可避免一次性读取内存不足,返回的是一个可迭代对象TextFileReader
。
指定 iterator=True 也可以返回一个可迭代对象 TextFileReader 。iterator=True 和 chunksize 可以同时指定使用。
chunker = pd.read_csv('train.csv', chunksize=100)
for i, chunk in enumerate(chunker):
print(i,' ',len(chunk))
0 100
1 100
2 100
3 100
4 100
5 100
6 100
7 100
8 91
再看一个合并数据
的代码:
import pandas as pd
df = [pd.read_csv('./data/data_' + str(i) + '.csv') for i in range(5)] #列表推导式
data = pd.concat(df, axis=0).reset_index(drop=True) # 合并
data.head()
data.tail()
当 axis = 0 时,pd.concat 实现列对齐
合并。
分块读取文件demo例程:
import feather
import pandas as pd
filePath = r'data_csv.csv'
def read_csv_feature(filePath):
# 读取文件
f = open(filePath, encoding='utf-8')
reader = pd.read_csv(f, sep=',', iterator=True)
loop = True
chunkSize = 1000000
chunks = []
while loop:
try:
chunk = reader.get_chunk(chunkSize)
chunks.append(chunk)
except StopIteration:
loop = False
print('Iteration is END!!!')
df = pd.concat(chunks, axis=0, ignore_index=True)
f.close()
return df
data = read_csv_feature(filePath)
二、初步观察
导入数据后需要对数据的整体结构
和样例进行概览,比如数据大小、格式等等。
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
df.head(10) #输出前10行数据
df.tail(15) #输出后15行数据
df.isnall().head() #判断数据是否为空
df.to_csv('train_chinese.csv')
# 注意:不同的操作系统保存下来可能会有乱码。大家可以加入`encoding='GBK' 或者 ’encoding = ’utf-8‘‘`
数据分析入门 | kaggle泰坦尼克任务 系列持续更新,欢迎
点赞收藏
+关注
上一篇:数据分析入门 | kaggle泰坦尼克任务
下一篇:数据分析入门 | kaggle泰坦尼克任务(二)—>pandas基础
本人水平有限,文章中不足之处欢迎下方评论区批评指正~如果感觉对你有帮助,点个赞 支持一下吧 ~
不定期分享 有趣、有料、有营养内容,欢迎 订阅关注 我的博客 ,期待在这与你相遇 ~
边栏推荐
- 3.1 leetcode daily question 6
- string null转空字符串(空字符串是什么意思)
- 移动端H5开发常用技巧总结
- 面试第二家公司的面试题及答案(二)
- Tradingview 使用教程
- [socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
- [Halcon vision] image filtering
- [Halcon vision] array
- mysql 进不去了怎么办
- 一些你不知道的 web API
猜你喜欢
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
Jpg to EPS
Basics of data communication - basic knowledge of network
PLC overview
Data communication foundation TCPIP reference model
[Halcon vision] morphological corrosion
Data communication foundation STP principle
404页面和路由钩子
[Halcon vision] image filtering
随机推荐
About automatic operation on Web pages
【Halcon视觉】图像的傅里叶变换
Prevent XSS attacks
抓包工具fiddler和wireshark对比
Learning about opencv (2)
利用原生js实现自定义滚动条(可点击到达,拖动到达)
函数模板参数(函数参数在哪)
Using undertow, Nacos offline logout delay after service stop
[Qualcomm][Network] qti服务分析
【C#语言】具名类型和匿名类型
Controller返回JSON数据
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
2022/07/25 ------ arrangement of strings
The CLOB field cannot be converted when querying Damon database
Learning about opencv (4)
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
[Halcon vision] image filtering
The charm of SQL optimization! From 30248s to 0.001s
Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
Common errors when starting projects in uniapp ---appid