API - 特征工程 - 读Excel、CSV

API官方文档:

pandas.read_excel
pandas.read_csv

一、读取Excel文件

3153092-661fb6d58d9bdf22.png
excel_countif

1、sheet_name表格的小表名

stu_info = pd.read_excel('student_info1.xlsx',sheetname='countif').head(2)
stu_info.columns.name='学科'
stu_info
3153092-b726921655a5da6d.png

2、 把哪一列当作行索引 index_col

stu_info = pd.read_excel('student_info1.xlsx',
  sheetname='countif',index_col=0).head(2)
stu_info.columns.name='学科'
stu_info
3153092-c8d50804c7bdaa73.png

3、 set_index("列名") 更改某一列为行索引

stu_info.set_index='学号'
stu_info

4、 reset_index(drop=True) 重置行索引,并把行索引转换为数据列
drop=True 不想要学号,即把行索引数据删除

stu_info.reset_index(drop=True)
3153092-0edd62b2755c7e3a.png

二、读取csv文件

3153092-c102813ce6988f91.png
csv文件可以用写字板打开

1、读取csv文件

stu_info = pd.read_csv('student_info1.csv')
stu_info
3153092-3f03a658a39e06ca.png

2、 从第I行开始作为列索引
header=None或数字
NONE 说明文件里面没有设置列索引,不把第一行当索引了
0,1 行当索引。默认是第0行

stu_info = pd.read_csv('student_info1.csv',header=1)
stu_info
3153092-55f2c301a0ffca27.png

3、 加列索引

stu_info = pd.read_csv('student_info1.csv',header=1,names=['语文','数学','英语'])
stu_info
3153092-39de5c8cf27b3c9b.png

4、编码 、解析引擎
encoding 编码 默认utf-8 Windows新建文件,gb2312,gbk
engine 解析引擎 c比较快 python支撑更多方法

stu_info = pd.read_csv('student_info1.csv',encoding='utf-8',engine='python')


3153092-2e922d9b8a7a31ee.png
01_家庭用电预测:线性回归算法(时间与功率&功率与电流之间的关系

如果没有混合类型的数据的时候,可以通过low_memory=False调用更多内存,提高读取速度。 混合类型

sep=';' 以';'分隔每行的数据。

path1 = 'datas/household_power_consumption_1000.txt'
df = pd.read_csv(path1, sep=';', low_memory=False)

猜你喜欢

转载自blog.csdn.net/weixin_33901641/article/details/87116059