微信扫一扫打赏支持

python操作excel库xlwings---7、格式化数据

python操作excel库xlwings---7、格式化数据

一、总结

一句话总结:

格式化数据也就是处理各个地级市之间欧式距离的数据,因为原来数据不好取,所以可以生成一个283*283的表,来表示城市之间的距离,这样取数据特别方便
# 1、从excel中读地级市的欧氏距离
# 2、规整数据,生成一个283*283的表
# 3、将规整化的数据写入excel中

 

 

二、格式化数据

博客对应课程的视频位置:7、格式化数据-范仁义-读书编程笔记
https://www.fanrenyi.com/video/34/314

 

 

1、示例需求分析

需求

更加直观一点


操作步骤

步骤一:先处理数据

  • 获取每个市的每一年的GDP(数据规整,不用处理)
  • 处理地级市欧式距离(数据不好取,可以生成一个283*283的表,来表示城市之间的距离)
  • 城市行政区域面积(数据规整,不用处理)

步骤二:按公式计算结果

 

2、格式化数据

处理地级市欧式距离(数据不好取,可以生成一个283*283的表,来表示城市之间的距离)

In [1]:
# 1、从excel中读地级市的欧氏距离
# 2、规整数据,生成一个283*283的表
# 3、将规整化的数据写入excel中
In [1]:
# 1、读地级市的欧氏距离
import xlwings as xw
# 应用->工作簿->工作表->范围
# 应用
app = xw.App(visible=True,add_book=False)
# 工作簿
wb = app.books.open("283地级市的欧氏距离.xlsx")
# 工作表
sht = wb.sheets["283地级市欧式直线距离(千米)"]
# 范围
data = sht.range("a2:i80090").value
# 保存excel
wb.save()
# 关闭excel程序
wb.close()
app.quit()
In [2]:
print(len(data))
print(data[0])
print(data[80088])
80089
['北京市', 1.0, 39.9361, 116.412, '北京市', 1.0, 39.9361, 116.412, 0.0]
['克拉玛依市', 283.0, 44.255, 84.9233, '克拉玛依市', 283.0, 44.255, 84.9233, 0.0]
In [3]:
# 2、规整数据,生成一个283*283的表
# 初始化一个283*283的表,值全是0
distance = [[0]*283 for _ in range(283)]
# 向这个表里面填值
for i in data:
    distance[int(i[1])-1][int(i[5])-1]=i[8]
print(distance[0])
[0.0, 113.55159, 265.54511, 155.59686, 420.93482, 157.13815, 143.63809, 185.61208, 273.29239, 360.21176, 72.063934, 174.07985, 255.85946, 400.76877, 268.99631, 339.23737, 506.9074, 582.09654, 353.71522, 367.80185, 401.78432, 520.05409, 607.82868, 720.92466, 1077.6528, 642.72005, 304.72902, 569.55172, 771.39544, 404.35707, 549.12164, 814.81998, 336.28898, 622.14835, 464.17816, 572.21667, 666.71767, 631.54415, 677.27193, 419.61065, 507.0681, 491.03964, 588.07546, 673.567, 395.483, 496.10045, 374.14996, 858.33409, 935.5966, 751.4122, 799.61527, 829.2616, 863.41415, 817.16861, 912.79872, 1032.3198, 1017.6205, 1208.058, 1360.2472, 1331.4003, 1383.6147, 1400.4162, 1344.5215, 1397.6375, 1324.1024, 1021.3557, 1135.5693, 1068.4431, 899.65153, 991.1792, 632.05377, 959.50607, 1032.3802, 970.98451, 635.18263, 742.69662, 793.96687, 881.0195, 904.02892, 885.1765, 684.0175, 1129.7395, 1098.1259, 1070.486, 1208.3862, 1237.2298, 1165.5139, 1376.8396, 1330.7966, 1315.2396, 1243.0507, 1211.6758, 1021.2535, 696.25243, 865.9783, 1048.0064, 783.88159, 918.47632, 900.11248, 781.1543, 807.87424, 1010.5855, 930.80812, 657.56961, 970.9819, 1047.6298, 1144.8835, 688.81245, 1556.596, 1513.4291, 1493.2961, 1490.1351, 1625.1999, 1678.1353, 1704.2924, 1636.7445, 1724.6075, 1247.5709, 1190.6819, 1385.5944, 1144.6421, 1347.069, 1298.0219, 1570.3379, 1359.0676, 1283.6904, 1429.4309, 1337.3816, 551.38429, 368.50586, 398.33544, 570.92206, 510.24856, 426.73045, 504.15061, 554.21535, 417.4608, 385.47144, 524.57654, 276.41571, 313.21971, 339.08477, 571.36334, 571.03416, 418.4545, 626.30566, 597.27107, 679.96788, 741.30806, 464.09225, 481.76737, 558.69699, 589.56535, 482.06411, 693.68046, 737.66084, 734.46905, 851.51807, 609.34522, 901.32938, 716.99551, 802.72765, 1049.4224, 1084.1899, 961.31963, 1127.0459, 1121.7782, 961.94912, 1091.3134, 1053.0422, 1020.2407, 1057.1947, 1133.9389, 937.56757, 1338.1375, 1368.5728, 1372.6423, 1487.4746, 1477.9567, 1210.9352, 1281.8968, 1315.219, 1306.9842, 1597.7743, 1601.7067, 1406.9062, 1498.8328, 1885.8252, 1938.1387, 1974.9457, 1847.2328, 1896.645, 1697.2198, 1804.5362, 1735.7316, 1875.421, 1903.7778, 1904.1601, 1955.9157, 1943.846, 2065.5548, 2146.473, 2092.9758, 1903.059, 1834.9989, 1805.4753, 1821.2569, 1922.2372, 2044.9129, 1850.6038, 1722.1293, 1885.1128, 2160.9305, 2164.4864, 2130.0011, 1970.6138, 2019.2125, 1790.6411, 2003.8945, 1874.4224, 1928.0575, 2106.9031, 2282.7111, 2481.9079, 1462.3828, 1520.9311, 1573.5993, 2020.6537, 1592.3217, 1454.8345, 1418.475, 1273.9659, 1452.71, 1547.859, 1628.3075, 1365.3574, 1637.7673, 1357.7871, 1251.5391, 1523.6513, 1588.3093, 1255.0368, 1645.6567, 1734.3285, 1819.0035, 1615.3317, 1802.0331, 2088.9153, 1830.2307, 1990.9823, 2165.4131, 2401.316, 2293.5612, 2100.4897, 2345.7382, 913.80157, 843.18318, 1037.9389, 926.00127, 853.09722, 1123.8936, 1033.9149, 886.34105, 695.60478, 608.16918, 1179.6747, 1558.4141, 1231.0344, 1136.0658, 1137.24, 1504.6241, 1373.2023, 1211.2134, 1140.3775, 977.36795, 881.77029, 1324.7039, 885.90898, 867.43552, 913.31887, 988.51175, 2395.3249, 2631.9362]
In [4]:
# 3、将规整化的数据写入excel中
# 应用->工作簿->工作表->范围
# 应用
app = xw.App(visible=True,add_book=False)
# 工作簿
wb = app.books.add()
# 工作表
sht = wb.sheets["sheet1"]
# 范围
sht.range("a1").value=distance
# 保存excel
wb.save("规整化的283地级市的欧氏距离.xlsx")
# 关闭excel程序
wb.close()
app.quit()

 

3、按公式计算结果

处理地级市欧式距离(数据不好取,可以生成一个283*283的表,来表示城市之间的距离)

In [1]:
# 1、从excel中读地级市的欧氏距离
# 2、规整数据,生成一个283*283的表
# 3、将规整化的数据写入excel中
In [1]:
# 1、读地级市的欧氏距离
import xlwings as xw
# 应用->工作簿->工作表->范围
# 应用
app = xw.App(visible=True,add_book=False)
# 工作簿
wb = app.books.open("283地级市的欧氏距离.xlsx")
# 工作表
sht = wb.sheets["283地级市欧式直线距离(千米)"]
# 范围
data = sht.range("a2:i80090").value
# 保存excel
wb.save()
# 关闭excel程序
wb.close()
app.quit()
In [2]:
print(len(data))
print(data[0])
print(data[80088])
80089
['北京市', 1.0, 39.9361, 116.412, '北京市', 1.0, 39.9361, 116.412, 0.0]
['克拉玛依市', 283.0, 44.255, 84.9233, '克拉玛依市', 283.0, 44.255, 84.9233, 0.0]
In [3]:
# 2、规整数据,生成一个283*283的表
# 初始化一个283*283的表,值全是0
distance = [[0]*283 for _ in range(283)]
# 向这个表里面填值
for i in data:
    distance[int(i[1])-1][int(i[5])-1]=i[8]
print(distance[0])
[0.0, 113.55159, 265.54511, 155.59686, 420.93482, 157.13815, 143.63809, 185.61208, 273.29239, 360.21176, 72.063934, 174.07985, 255.85946, 400.76877, 268.99631, 339.23737, 506.9074, 582.09654, 353.71522, 367.80185, 401.78432, 520.05409, 607.82868, 720.92466, 1077.6528, 642.72005, 304.72902, 569.55172, 771.39544, 404.35707, 549.12164, 814.81998, 336.28898, 622.14835, 464.17816, 572.21667, 666.71767, 631.54415, 677.27193, 419.61065, 507.0681, 491.03964, 588.07546, 673.567, 395.483, 496.10045, 374.14996, 858.33409, 935.5966, 751.4122, 799.61527, 829.2616, 863.41415, 817.16861, 912.79872, 1032.3198, 1017.6205, 1208.058, 1360.2472, 1331.4003, 1383.6147, 1400.4162, 1344.5215, 1397.6375, 1324.1024, 1021.3557, 1135.5693, 1068.4431, 899.65153, 991.1792, 632.05377, 959.50607, 1032.3802, 970.98451, 635.18263, 742.69662, 793.96687, 881.0195, 904.02892, 885.1765, 684.0175, 1129.7395, 1098.1259, 1070.486, 1208.3862, 1237.2298, 1165.5139, 1376.8396, 1330.7966, 1315.2396, 1243.0507, 1211.6758, 1021.2535, 696.25243, 865.9783, 1048.0064, 783.88159, 918.47632, 900.11248, 781.1543, 807.87424, 1010.5855, 930.80812, 657.56961, 970.9819, 1047.6298, 1144.8835, 688.81245, 1556.596, 1513.4291, 1493.2961, 1490.1351, 1625.1999, 1678.1353, 1704.2924, 1636.7445, 1724.6075, 1247.5709, 1190.6819, 1385.5944, 1144.6421, 1347.069, 1298.0219, 1570.3379, 1359.0676, 1283.6904, 1429.4309, 1337.3816, 551.38429, 368.50586, 398.33544, 570.92206, 510.24856, 426.73045, 504.15061, 554.21535, 417.4608, 385.47144, 524.57654, 276.41571, 313.21971, 339.08477, 571.36334, 571.03416, 418.4545, 626.30566, 597.27107, 679.96788, 741.30806, 464.09225, 481.76737, 558.69699, 589.56535, 482.06411, 693.68046, 737.66084, 734.46905, 851.51807, 609.34522, 901.32938, 716.99551, 802.72765, 1049.4224, 1084.1899, 961.31963, 1127.0459, 1121.7782, 961.94912, 1091.3134, 1053.0422, 1020.2407, 1057.1947, 1133.9389, 937.56757, 1338.1375, 1368.5728, 1372.6423, 1487.4746, 1477.9567, 1210.9352, 1281.8968, 1315.219, 1306.9842, 1597.7743, 1601.7067, 1406.9062, 1498.8328, 1885.8252, 1938.1387, 1974.9457, 1847.2328, 1896.645, 1697.2198, 1804.5362, 1735.7316, 1875.421, 1903.7778, 1904.1601, 1955.9157, 1943.846, 2065.5548, 2146.473, 2092.9758, 1903.059, 1834.9989, 1805.4753, 1821.2569, 1922.2372, 2044.9129, 1850.6038, 1722.1293, 1885.1128, 2160.9305, 2164.4864, 2130.0011, 1970.6138, 2019.2125, 1790.6411, 2003.8945, 1874.4224, 1928.0575, 2106.9031, 2282.7111, 2481.9079, 1462.3828, 1520.9311, 1573.5993, 2020.6537, 1592.3217, 1454.8345, 1418.475, 1273.9659, 1452.71, 1547.859, 1628.3075, 1365.3574, 1637.7673, 1357.7871, 1251.5391, 1523.6513, 1588.3093, 1255.0368, 1645.6567, 1734.3285, 1819.0035, 1615.3317, 1802.0331, 2088.9153, 1830.2307, 1990.9823, 2165.4131, 2401.316, 2293.5612, 2100.4897, 2345.7382, 913.80157, 843.18318, 1037.9389, 926.00127, 853.09722, 1123.8936, 1033.9149, 886.34105, 695.60478, 608.16918, 1179.6747, 1558.4141, 1231.0344, 1136.0658, 1137.24, 1504.6241, 1373.2023, 1211.2134, 1140.3775, 977.36795, 881.77029, 1324.7039, 885.90898, 867.43552, 913.31887, 988.51175, 2395.3249, 2631.9362]
In [4]:
# 3、将规整化的数据写入excel中
# 应用->工作簿->工作表->范围
# 应用
app = xw.App(visible=True,add_book=False)
# 工作簿
wb = app.books.add()
# 工作表
sht = wb.sheets["sheet1"]
# 范围
sht.range("a1").value=distance
# 保存excel
wb.save("规整化的283地级市的欧氏距离.xlsx")
# 关闭excel程序
wb.close()
app.quit()

 

 

 

 
posted @ 2020-07-06 05:34  范仁义  阅读(475)  评论(0编辑  收藏  举报