temp1.append(df['销售订单数量'][i].replace(',','')) 5 temp2.append(df['交货数量'][i].replace(',','')) 6 df['销售订单数量']=temp1 AttributeError: 'numpy.float64' object has no attribute 'replace'具体要如何操作?
时间: 2023-09-03 19:08:49 浏览: 95
这个错误是因为在执行`replace()`方法时,传入了一个`numpy.float64`类型的对象,而该对象并没有`replace()`方法。您需要将其转换为字符串类型,然后再执行`replace()`方法。您可以使用以下代码来解决这个问题:
```
temp1.append(str(df['销售订单数量'][i]).replace(',',''))
temp2.append(str(df['交货数量'][i]).replace(',',''))
df['销售订单数量'] = temp1
```
这里将`df['销售订单数量'][i]`和`df['交货数量'][i]`强制转换为字符串类型,然后才执行`replace()`方法。
相关问题
temp1=[] temp2=[] for i in range(len(df)): temp1.append(str(df['销售订单数量'][i]).replace(',','')) temp2.append(df['交货数量'][i].replace(',','')) df['销售订单数量']=temp1 df['交货数量']=temp2 df.to_csv('G:/作业/机器学习2/项目实训包/第8章 汽车备件销售预测/数据/sales_erase.csv',index=None)这是否正确
这段代码的目的是将DataFrame中的`'销售订单数量'`和`'交货数量'`列中的逗号去掉,并将处理后的数据写入到CSV文件中。这段代码的实现方式是正确的。
不过需要注意的是,在第6行的代码中,`df['交货数量'][i]`应该也需要转换为字符串类型,以便执行`replace()`方法。修改后的代码如下:
```
temp1 = []
temp2 = []
for i in range(len(df)):
temp1.append(str(df['销售订单数量'][i]).replace(',', ''))
temp2.append(str(df['交货数量'][i]).replace(',', ''))
df['销售订单数量'] = temp1
df['交货数量'] = temp2
df.to_csv('G:/作业/机器学习2/项目实训包/第8章 汽车备件销售预测/数据/sales_erase.csv', index=None)
```
这样修改之后,就可以正确地将处理后的数据写入到CSV文件中了。
找出以下代码的错误并改正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)
代码中有以下问题:
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)
```
阅读全文