使用Python对excel中的数据进行处理

一、读取excel中的数据

首先引入pandas库,没有的话使用控制台安装 —— pip install pandas

import pandas as pd     #引入pandas库,别名为pd

#read_excel用于读取excel中的数据,这里只列举常用的两个参数(文件所在路径,忽略头字段)
data = pd.read_excel('excel路径', header = 0)
print(data)  #可以打印看看自己读取的数据

read_excel的具体参数,可根据实际需要选择。

二、转换和处理数据

如果想对excel中读取的数据进行处理,最好把它转换成list,输出的时候使用dataFrame。

#输入之后转换一维数组
data = datas.values  #只读取excel中的值,不读取序号
data = list(np.concatenate(data.reshape((-1, 1), order="F")))  #转换
print(list)  #查看数组

#其他处理代码……

#输出的时候,定义一个空的dataFrame,把数据添加到dataFrame中
df = pd.DataFrame()  #定义空的dataFrame
#通过循环将数据添加到df
for i in data:
    df = df.append([i])   #把数据添加到末尾

三、将数据输出到excel

输出同样只要一行简单代码,使用dataFrame格式将数据输出到表格。

#to_excel用于输出excel中的数据,这里同样只列举两个常用参数(文件输出路径,忽略头字段)
df.to_excel('输出路径', index = False)

to_excel的具体参数,可根据实际需要选择。

四、使用pyhon处理excel数据的简单示例(含详细注释)

示例中的excel只有一列数据,主要作用处理是读取excel中的数据将之提取为纯中文字符串——使用正则表达式匹配。

大家可以根据自己的实际需要对数据进行转换和处理。

import pandas as pd
import numpy as np
import re  #正则

datas = pd.read_excel('old.xlsx', header = 0)  #从excel中读取数据(这里使用的是相对路径)

data = datas.values  #只读取excel中的值,不读取序号

resource = list(np.concatenate(data.reshape((-1, 1), order="F")))  #将读取的数据转换为list

# print(resource)  #打印list

#提取中文字符串函数
def chinese(s):
    # res = re.findall('[^0-9]', s)  #使用正则表达式匹配非数字的字符
    res = re.findall('[\u4e00-\u9fa5]', s)      #使用正则表达式匹配中文字符
    return ''.join(res)     #将字符拼接成字符串

df = pd.DataFrame()  #定义空的dataFrame

#依次读取list中的数据,将之处理为纯中文字符串
for i in resource:
    i = str(i)      #excel中的部分数据为非字符串,这里全部转换为字符串
    ch = chinese(i)     #提取中文字符串
    df = df.append([ch])   #将数据添加到df

df.to_excel('new.xlsx', index = False)  #将处理后的数据输出到excel表格

猜你喜欢

转载自blog.csdn.net/weixin_49851451/article/details/129255265