当前位置: 首页 > news >正文

网上购物网站网站建设分析网站优化及推广方案

网上购物网站网站建设分析,网站优化及推广方案,上海网优化seo公司,php动态网站开发外文文献读取合并单元格并保留合并信息 读取合并单元格并保留合并信息清除各单元格的空格和换行符,并去除列名中的空格和换行符 读取合并单元格并保留合并信息 当我们只是使用 pandas 的 read_excel 方法读取 Excel 文件时,我们可能会遇到一个很棘手的问题&…

读取合并单元格并保留合并信息

  • 读取合并单元格并保留合并信息
  • 清除各单元格的空格和换行符,并去除列名中的空格和换行符

读取合并单元格并保留合并信息

当我们只是使用 pandas 的 read_excel 方法读取 Excel 文件时,我们可能会遇到一个很棘手的问题:合并单元格的信息将会丢失,从而导致我们的数据出现重复或缺失的情况。

在本篇文章中将介绍使用 pandas 正确地读取包含合并单元格的 Excel 表格,支持 xlsx 和 xls。

import pandas as pd
from openpyxl import load_workbook
from xlrd import open_workbookdef read_xlsx(file, sheet_name=None, header=None):"""读取 xlsx 格式文件。"""excel = pd.ExcelFile(load_workbook(file), engine="openpyxl")sheet_name = sheet_name or excel.sheet_names[0]sheet = excel.book[sheet_name]df = excel.parse(sheet_name, header=header)for item in sheet.merged_cells:top_col, top_row, bottom_col, bottom_row = item.boundsbase_value = item.start_cell.value# 1-based index转为0-based indextop_row -= 1top_col -= 1# 由于前面的几行被设为了header,所以这里要对坐标进行调整if header is not None:top_row -= header + 1bottom_row -= header + 1df.iloc[top_row:bottom_row, top_col:bottom_col] = base_valuereturn dfdef read_xls(file, sheet_name=None, header=None):"""读取 xls 格式文件。"""excel = pd.ExcelFile(open_workbook(file, formatting_info=True), engine="xlrd")sheet_name = sheet_name or excel.sheet_names[0]sheet = excel.book[sheet_name]df = excel.parse(sheet_name, header=header)# 0-based indexfor top_row, bottom_row, top_col, bottom_col in sheet.merged_cells:base_value = sheet.cell_value(top_row, top_col)# 由于前面的几行被设为了header,所以这里要对坐标进行调整if header is not None:top_row -= header + 1bottom_row -= header + 1df.iloc[top_row:bottom_row, top_col:bottom_col] = base_valuereturn df

注:来源https://alanlee.fun/2023/04/27/pandas-read-excel-with-merged-cells/

清除各单元格的空格和换行符,并去除列名中的空格和换行符

在数据处理过程中,字符串中的多余空格和换行符常常会影响数据的整洁性以及后续分析。使用 .replace(‘\n’, ‘’).strip() 可以有效地去除换行符和前后空格,但这并不能解决中间空格的问题。为了解决这一问题,,通过使用字符串处理方法实现的 remove_spaces 函数能够高效地去除 Pandas DataFrame 中每个单元格及其列名的空格和换行符,同时也会移除字符串中的所有空格(包括字与字之间的空格)


def remove_spaces(df):"""去除 DataFrame 中各单元格的空格和换行符,并去除列名中的空格和换行符。"""# 处理列名df.columns = [col.replace('\n', '').strip() if isinstance(col, str) else col for col in df.columns]# 处理各单元格,去掉所有空格,包括中间的空格和换行符return df.apply(lambda col: col.map(lambda x: x.replace('\n', '').replace(' ', '') if isinstance(x, str) else x))
http://www.wangmingla.cn/news/29342.html

相关文章:

  • python做网站开发软文媒体
  • 网站开发的目的和意义白杨seo课程
  • 网站后台分类游戏推广代理加盟
  • 学校网站功能描述百度网址大全旧版本
  • 创意网站特效十大最靠谱it培训机构
  • 电子商务与网站建设实践论文优化大师apk
  • 怎么做网站聊天视频直播网络营销专业就业前景
  • seo网站优化教程太原关键词排名推广
  • 网站开发者工具新闻稿范文
  • 班级网页网站建设济宁seo优化公司
  • wordpress 搜索表单东莞seo软件
  • 北京网站制作找客户资源的软件
  • 网站后台管理教程免费seo优化
  • 群晖做网站需要备案吗企业如何进行网络推广
  • 做本地团购网站怎么样合肥网络推广公司
  • 在百度云上建设网站品牌策略
  • 四网合一的网站上海网络seo
  • dede5.7网站搬家西安seo网站关键词
  • 无锡论坛网本地网站关于seo的行业岗位有哪些
  • 网站建设专员工作什么关键词可以搜到那种
  • 天津建设工程网站seo优化培训公司
  • 衡阳网站建设制作seo免费软件
  • ps做网站图片水印武汉谷歌seo
  • 做公司网站教程视频超级软文网
  • 微信 网站提成方案点做站长工具seo综合查询烟雨楼
  • 延庆网站建设师说说seo论坛
  • 网站建设政府采购腾讯广告投放推广平台价格
  • 网店怎么开店注册长沙seo推广
  • 东营网站建设培训学校seo 知乎
  • 网站开发课程论文网络营销师培训费用是多少