优化这段代码import pandas as pd import xlrd import matplotlib.pyplot as plt import numpy as np table1=pd.read_excel("grouped_order_sale.xlsx",sheet_name='Sheet1') row1=pd.read_excel("grouped_order_sale.xlsx",sheet_name='Sheet1') for i in range(len(row1)): if row1[i]=='first_cate_code': oneindex1=i elif row1[i]=='ord_qty': oneindex2=i firstcate=table1.col_values(oneindex1,1) ord=table1.col_values(oneindex2,1) Firstcate=list(sorted(set(firstcate))) Ord_Number=[] for i in range(len(Firstcate)): sum=0 for j in range(len(ord)): if(firstcate[j]==Firstcate(j)): sum+=ord[j] Ord_Number.append(int(sum)) plt.xlabel('first_cate_code') plt.ylabel('ord_qty') plt.bar(Firstcate,Ord_Number) for a,b in zip(left,height): plt.text(a,b+1,b,ha='center',va='bottom') plt.show()
时间: 2023-06-06 12:06:31 浏览: 210
python数据分析与可视化 import pandas as pd import numpy as np import m
我可以帮你优化这段代码。首先,你可以通过一次读取数据来替换两次读取数据,如下所示:
```import pandas as pd
import matplotlib.pyplot as plt
table1 = pd.read_excel("grouped_order_sale.xlsx", sheet_name='Sheet1')
row1 = table1.columns.tolist()
for i in range(len(row1)):
if row1[i] == 'first_cate_code':
oneindex1 = i
elif row1[i] == 'ord_qty':
oneindex2 = i
firstcate = table1.iloc[:, oneindex1].tolist()
ord_qty = table1.iloc[:, oneindex2].tolist()
Firstcate = sorted(set(firstcate))
Ord_Number = []
for i in range(len(Firstcate)):
sum = 0
for j in range(len(ord_qty)):
if firstcate[j] == Firstcate[i]:
sum += ord_qty[j]
Ord_Number.append(int(sum))
plt.xlabel('first_cate_code')
plt.ylabel('ord_qty')
plt.bar(Firstcate, Ord_Number)
for a, b in zip(Firstcate, Ord_Number):
plt.text(a, b+1, b, ha='center', va='bottom')
plt.show()```
这段代码使用了 `iloc`来获取指定的列,从而避免了使用循环来遍历整个数据集。此外,我还移除了无用的导入语句和将列表排序的步骤。
希望这可以帮到你!
阅读全文