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 12:08:49 浏览: 39
这个错误是因为在执行`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文件中了。
df.append(1,axis=0)正确吗
不正确。因为 `append()` 方法的语法是 `DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)`,其中 `axis` 参数应该是指定行或列方向。如果要添加一行数据,应该将 `axis` 参数设置为 0,然后应该使用 `pd.Series` 或者 `pd.DataFrame` 对象作为参数传递给 `append()` 方法。例如,可以这样写:
```
df = df.append(pd.Series([1,2,3]), ignore_index=True)
```
这将在 `df` 的最后添加一行数据 [1,2,3]。