没有合适的资源?快使用搜索试试~ 我知道了~
使用pandas进行excel文件的读取写入
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
温馨提示
pandas的DateFrame类似于excel的数据储存结构,故为了便于pandas进行数据处理,设计者提供了简便的方法进行数据的读取,下面写上代码 import pandas as pd import numpy as np def save_excel(arr, exc_filename=None): #读取excel的函数 Save the data in excel format if exc_filename == None: exc_filename = excel.xlsx arr_df = pd.DataFr
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/14856723/bg1.jpg)
使用使用pandas进行进行excel文件的读取写入文件的读取写入
pandas的DateFrame类似于excel的数据储存结构,故为了便于pandas进行数据处理,设计者提供了简便的方法进行数据的读
取,下面写上代码
import pandas as pd
import numpy as np
def save_excel(arr, exc_filename=None): #读取excel的函数
"""Save the data in excel format"""
if exc_filename == None:
exc_filename = "excel.xlsx"
arr_df = pd.DataFrame(arr)
np_arr = np.array(arr)
arr_r, arr_c = np_arr.shape
arr_r_b = len(str(arr_r)) #行数的个数,3位数还是4位数
arr_c_b = len(str(arr_c))
list_index = [] list_columns = [] for ii in np.arange(arr_r):
x_str = 'Y' + str(ii).zfill(arr_r_b) # The number of digits is not enough to automatically zero
list_index.append(x_str)
arr_df.index = list_index #建立列索引
for ii in np.arange(arr_c):
y_str = 'X' + str(ii).zfill(arr_c_b)
list_columns.append(y_str)
arr_df.columns = list_columns #建立行索引
writer = pd.ExcelWriter(exc_filename)
arr_df.to_excel(writer, 'page_1', float_format='%.8f')
writer.save()
df = pd.read_excel(r"excel.xlsx",sheet_nam = 0,index_col = 0,head=0,nrows = 5)
#sheet_name: excel文件中的表名
#index_col: 使用哪一列作为行索引,默认从0开始
#header: 哪一行设置为列索引,默认是第一行,即header = 0
#usecols: 读取表格中哪几列,必须是位置索引
#date_parser:解析日期的函数
#parse_dates:尝试将数据解析为日期,默认为False。如果为True,则尝试解析所有列。此外,还可以指定需要解析的一组
列号或者列名
#names: 列索引
#engine: 默认是C,如文件路径存在中文时,engine= "python"
#encoding: 默认是utf-8,还可以是gbk
#skiprows: 跳过前几行读取文件,默认从0开始
#nrows: 读取多少行数据
#converters: 列名跟函数之间的映射关系组成的字典
下面是附加对csv文件的操作
def save_csv(arr,csv_filename=None):
"""save data in sv format"""
if csv_filename == None:
csv_filename = 'csv.csv'
arr_df = pd.DataFrame(arr)
arr_df.to_csv(csv_filename,float_format='%.3f',index=False,header =False)
df = pd.read_csv(r"D:\迅雷下载\示例txt.csv",encoding = "gbk",engine = "python",nrows= 5,index_col = "用户id")
借鉴:python学习之路–pandas读写文件
作者:python的男人
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38565003
- 粉丝: 6
- 资源: 913
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论1