python读取excel、csv

1、python3中使用 pandas读取csv

pandas.read_csv的函数说明参考函数参数说明

该函数主要的参数为io、sheetname、header、names、encoding。

io:csv文件,可以是文件路径、file-like对象;

sheetname:返回指定的sheet,参数可以是字符串、整型、list、none(返回字典,全部sheet);

header:指定数据表的表头,参数可以是int、list of ints,即为索引行数为表头;

names:返回指定name的列,参数为array-like对象。

encoding:关键字参数,指定以何种编码读取。

下面直接给出一个例子,说明使用方法。文件是机器学习中很常见的鸢尾花数据集:点击下载

# -*- coding: utf-8 -*-
"""

@author: Administrator
"""

import pandas as pd


dataframe =  pd.read_csv("iris.csv", header=None)
dataset = dataframe.values
X = dataset[:, 0:4].astype(float)
Y = dataset[:, 4]

print(X.shape)
print(Y.shape)
print(Y)

程序运行结果为:

 2、 pandas读取csv

pandas.read_excel的函数说明参考函数参数说明

用法和pandas.read_csv差不多,不举例了。

猜你喜欢

转载自blog.csdn.net/qq_27871973/article/details/81087881