找出以下代码的错误并改正import pandas as pd df=pd.read_csv("D:/qcbj/sj/sales_details.csv") df1=df.dropna() df1.to_csv('D:/qcbj/sj/sales_dropna.csv',index=None) count=0 for i in range(len(df)): if (df['应发库'][i]!="sz" and df['应发库'][i]!="cs" and df['应发库'][i]!="sy" and df['应发库'][i]!="sh" and df['应发库'][i]!="cd" and df['应发库'][i]!="xa" and df['应发库'][i] !="km" and df['应发库'][i]!="jn" and df['应发库'][i]!="bj"): count+=1 print (count) droplist=[] for i in range(len(df)): if (df['应发库'][i]!="sz"and df['应发库'][i]!="cs"and df['应发库'][i]!="sy"and df['应发库'][i]!="sh"and df['应发库'][i]!="cd"and df['应发库'][i]!="xa"and df['应发库'][i]!="km"and df['应发库'][i]!="jn"and df['应发库'][i]!="bj"): droplist.append(i) df2=df1.drop(labels=droplist,axis=0) temp1=[] temp2=[] for i in range(len(df)): temp1.append(df['销售订单数量'][i].replace(',','')) temp2.append(df['销售订单数量'][i].replace(',','')) df['销售订单数量']=temp1 df['销售订单数量']=temp2 x=pd.factorize(df['物料编号']) y=pd.factorize(df['应发库']) df['物料编号']=x[0] df['应发库']=y[0] year=[] month=[] for i in range(len(df)): year.append(int(df['创建日期'][i][0:4])) month.append(int(df['创建日期'][i][5:7])) df['创建年']=year df['创建月']=month df import numpy as np df_array=np.array(df)
时间: 2023-10-07 17:11:03 浏览: 128
代码中有以下问题:
1. 在执行 `temp2.append(df['销售订单数量'][i].replace(',',''))` 时,应该替换的是 `df['发货数量'][i]` 而不是 `df['销售订单数量'][i]`。
2. 在执行 `df['销售订单数量']=temp1` 和 `df['销售订单数量']=temp2` 时,应该将 `df['销售订单数量']` 和 `df['发货数量']` 两列分别赋值为 `temp1` 和 `temp2`。
3. 在执行 `x=pd.factorize(df['物料编号'])` 和 `y=pd.factorize(df['应发库'])` 时,应该使用 df1 而不是 df。
4. 在执行 `year.append(int(df['创建日期'][i][0:4]))` 和 `month.append(int(df['创建日期'][i][5:7]))` 时,应该使用 df1 而不是 df。
5. 在执行 `df_array=np.array(df)` 时,应该使用 df1 而不是 df。
以下是修改后的代码:
```python
import pandas as pd
df = pd.read_csv("D:/qcbj/sj/sales_details.csv")
df1 = df.dropna()
df1.to_csv('D:/qcbj/sj/sales_dropna.csv', index=None)
count = 0
for i in range(len(df)):
if (df['应发库'][i] != "sz" and df['应发库'][i] != "cs" and df['应发库'][i] != "sy" and df['应发库'][i] != "sh" and df['应发库'][i] != "cd" and df['应发库'][i] != "xa" and df['应发库'][i] != "km" and df['应发库'][i] != "jn" and df['应发库'][i] != "bj"):
count += 1
print(count)
droplist = []
for i in range(len(df)):
if (df['应发库'][i] != "sz" and df['应发库'][i] != "cs" and df['应发库'][i] != "sy" and df['应发库'][i] != "sh" and df['应发库'][i] != "cd" and df['应发库'][i] != "xa" and df['应发库'][i] != "km" and df['应发库'][i] != "jn" and df['应发库'][i] != "bj"):
droplist.append(i)
df2 = df1.drop(labels=droplist, axis=0)
temp1 = []
temp2 = []
for i in range(len(df)):
temp1.append(df['销售订单数量'][i].replace(',', ''))
temp2.append(df['发货数量'][i].replace(',', ''))
df1['销售订单数量'] = temp1
df1['发货数量'] = temp2
x = pd.factorize(df1['物料编号'])
y = pd.factorize(df1['应发库'])
df1['物料编号'] = x[0]
df1['应发库'] = y[0]
year = []
month = []
for i in range(len(df1)):
year.append(int(df1['创建日期'][i][0:4]))
month.append(int(df1['创建日期'][i][5:7]))
df1['创建年'] = year
df1['创建月'] = month
df_array = np.array(df1)
```
阅读全文