本篇文章为大家展示了使用python怎么删除excel中的空值,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。
import xlrd
import pandas as pd
import openpyxl
target_xls = "合并表1.xlsx"
source_xls = ["全1.xlsx", "全2.xlsx","全3.xlsx",\
"全4.xlsx","全5.xlsx","全6.xlsx"]
sysptoms=pd.DataFrame()
for i in range(len(source_xls)):
print(i)#了解打印进度
sheet2=pd.read_excel(source_xls[i]).fillna("")#有空格,填充函数,填的空值。要加fillna,不然无法删除空值所对应的行
sysptom = sheet2[sheet2['电话'] !=""]#筛选
sysptoms=pd.concat([sysptoms,sysptom])#两个dataframe合并,相当于合并excel
print(type(sysptom))
sysptoms.to_excel(target_xls, index=False)#pandas写入excel用.to_excel
print("ok")
补充:python 读取excel数据,遇到空单元格的处理方法
读取excel表格时,经常遇到空单元格的情况,这时需要明确的是,空单元格在python中是什么格式,NULL?NAN还是什么?
在用 xlrd 函数读入excel时,空单元格其实是空字符串'' 形式
infilename = r'D:\aajja.xlsx'
workbook = xlrd.open_workbook(infilename)
df = workbook.sheet_by_name('sheetname')
num_rows = df.nrows - 1 # 我这里是第一行不要,所以跳过了
num_cols = df.ncols
t = 0
im_data = np.zeros((num_rows, num_cols))
for curr_row in range(1, num_rows+1):
for curr_col in range(num_cols):
rawVal = df.cell(curr_row, curr_col).value
if isinstance(rawVal, str):
im_data[curr_row - 1, curr_col] = np.nan
else:
im_data[curr_row - 1, curr_col] = float(rawVal)
if isinstance(rawVal, str)
判断该单元格数值是否为字符串,当然如果你的excel中本来就有字符串格式数据,这里可以更改为判断是否为空字符串,稍微修改一下即可
上述内容就是使用python怎么删除excel中的空值,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。