Python使用 pandas操作Excel文件并新增列数据

业务实现:对于原先的excel文件,我需要新增一个数据列,不同情况,列数据的值不同,分别为空白行、已爬取、已改名、爬取异常、改名未知异常

python 复制代码
# -*- coding: utf-8 -*-
import io
import os
import re
import sys

import numpy as np
import pandas as pd
import pandas.io.formats.excel
pandas.io.formats.excel.ExcelFormatter.header_style = None  # 表头不加粗

reload(sys)
sys.setdefaultencoding('utf-8')

def change_data(data):
    try:
        data = data.replace(' ', '')  # 删除空格
        data = re.sub(r'\([^)]*\)', '', data)  # 删除括号以及内容
        data = re.sub(r'\*', '', data)  # 删除*号
        return data
    except Exception:
        pass

# 文件夹名称
dir_name_set = set()
for item in os.listdir(unicode(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'country'), 'utf-8')):
    item = change_data(item)
    dir_name_set.add(item)

# 改名字典
spacial_name_dict = {}
with io.open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'spacial_country.txt'), 'r', encoding='utf-8') as f:
    lines = f.readlines()
    for line in lines:
        line_split = line.strip().split(' ')
        name1 = change_data(line_split[0])
        if '/' in name1:
            name1 = name1.split('/')[0]
        name2 = change_data(line_split[1])
        if '/' in name2:
            name2 = name2.split('/')[0]
        spacial_name_dict[name1] = name2

filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'country.xlsx')
sheet_name='species_data'
df = pd.read_excel(filename, sheet_name=sheet_name)
dfc = df.copy()
idx = 1

# 处理每一行数据
for index, row in dfc.iterrows():  # 返回每一行的索引和对应的行数据
    name = row['name']
    try:
        if isinstance(name, float) and np.isnan(name):
            dfc.loc[index, 'status'] = '空白行'
        else:
            name = change_data(name)
            if name in dir_name_set:
                dfc.loc[index, 'status'] = '已爬取'
            elif name in spacial_name_dict.keys():
                if spacial_name_dict[name] in dir_name_set:
                    dfc.loc[index, 'status'] = '已改名为{}'.format(spacial_name_dict[name])
                else:
                    dfc.loc[index, 'status'] = '改名未知异常'
            else:
                dfc.loc[index, 'status'] = '爬取异常'
        if (index != 0 and index % 10000 == 0) or index == len(dfc) - 1:
            print index, idx
            if idx == 15:
                dfcc = dfc.copy()
                output_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'country_{}.xlsx'.format(idx))
                print 'output_path', output_path
                dfcc.to_excel(output_path, sheet_name=sheet_name, index=False, header=True)
            idx += 1
    except Exception as e:
        print name, e
相关推荐
serve the people2 分钟前
tensorflow tf.nn.softmax 核心解析
人工智能·python·tensorflow
癫狂的兔子10 分钟前
【BUG】【Python】eval()报错
python·bug
啃火龙果的兔子10 分钟前
java语言基础
java·开发语言·python
masterqwer10 分钟前
day42打卡
python
不会飞的鲨鱼13 分钟前
抖音验证码滑动轨迹原理(很难审核通过)
javascript·python
我命由我1234513 分钟前
Python 开发问题:No Python interpreter configured for the project
开发语言·后端·python·学习·pycharm·学习方法·python3.11
多米Domi01122 分钟前
0x3f第12天 0-1背包
python·算法·leetcode·动态规划
电商API&Tina34 分钟前
跨境电商速卖通(AliExpress)数据采集与 API 接口接入全方案
大数据·开发语言·前端·数据库·人工智能·python
工业互联网专业42 分钟前
基于python的旅游景点推荐系统_flask+spider
python·flask·毕业设计·源码·课程设计·spider·旅游景点推荐系统
编程乐学(Arfan开发工程师)43 分钟前
信息收集与分析详解:渗透测试的侦察兵 (CISP-PTE 核心技能)
java·开发语言·javascript·python