python对excel的数据处理

主要分为3个步骤

1.从表中读入数据

2.对数据计算处理

3.写入新表

import re
import xlrd
import xlwt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#filename = filename.decode('utf-8') 中文转码
data= xlrd.open_workbook('C:/Users/cywti/Desktop/16Mudan.xlsx')
table = data.sheets()[0]
#通过索引顺序获取
#数一共由多少行,多少列用cols
nrows = table.nrows
print(nrows)

x=table.col_values(1)
y=table.col_values(2)
z=table.col_values(3)
s=table.col_values(3)
SZ=np.array([x[2::1],y[2::1],z[2::1],s[2::1]])
print(SZ)

list1=[]
list2=[]
list3=[]
list4=[]
for i in range(697):
    X=SZ[0,i]
    Y=SZ[1,i]
    Z=SZ[2,i]
    S=SZ[3,i]
    XX=str(X)
    YY=str(Y)
    ZZ=str(Z)
    print(S)
    list1.append(X,)
    list2.append(Y,)
    list3.append(Z,)
    list4.append(S,)

list1 = list(map(float, list1))
list2 = list(map(float, list2))
list3 = list(map(float, list3))
list4 = list(map(float, list4))
fig = plt.figure()

ax = fig.add_subplot(111, projection = '3d')
ax.scatter(list1,list2,list3)


plt.show()
SS=[]
for i in range(18):
    list4[17*i:17*(i+1)]
    print(list4[17*i:17*(i+1)])
    P=np.sum(list4[17*i:17*(i+1)])/17
    SS.append(P,)

print(SS)

workbook = xlwt.Workbook()
sheet = workbook.add_sheet("sheet1")
row1 = sheet.row(1)
for i in range(18):
    row1.write(i,SS[i])

workbook.save("11111.xls")

处理数据形式如下

结果如下

发布了30 篇原创文章 · 获赞 21 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/cywtiancai/article/details/81533616