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)这段代码报KeyError: '[357143] not found in axis'的原因
时间: 2023-08-30 13:06:12 浏览: 231
这个错误通常是由于尝试删除不存在于数据框中的行所引起的。具体来说,代码中 droplist 列表中的索引可能包含了数据框 df1 中不存在的行索引,导致在执行 `drop()` 方法时出现 KeyError 错误。
你可以添加以下代码来检查 droplist 列表中的索引是否超出了 df1 的行数范围:
```python
if max(droplist) >= len(df1):
print("索引超出范围")
```
如果输出结果为 “索引超出范围”,则需要检查代码中的索引是否正确。
相关问题
找出以下代码的错误并改正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)
```
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_12396\356241790.py in <module> 3 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"): 4 droplist.append(i) ----> 5 df2=df1.drop(labels=droplist,axis=0) ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs) 309 stacklevel=stacklevel, 310 ) --> 311 return func(*args, **kwargs) 312 313 return wrapper ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors) 4911 level=level, 4912 inplace=inplace, -> 4913 errors=errors, 4914 ) 4915 ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors) 4148 for axis, labels in axes.items(): 4149 if labels is not None: -> 4150 obj = obj._drop_axis(labels, axis, level=level, errors=errors) 4151 4152 if inplace: ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors) 4183 new_axis = axis.drop(labels, level=level, errors=errors) 4184 else: -> 4185 new_axis = axis.drop(labels, errors=errors) 4186 result = self.reindex(**{axis_name: new_axis}) 4187 ~\AppData\Local\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors) 6015 if mask.any(): 6016 if errors != "ignore": -> 6017 raise KeyError(f"{labels[mask]} not found in axis") 6018 indexer = indexer[~mask] 6019 return self.delete(indexer) KeyError: '[357143] not found in axis'
这个错误是由于你试图删除一个不存在的索引标签引起的。你需要检查代码,确保你要删除的标签是存在的,并且没有重复的标签。如果你不确定要删除的标签是否存在,你可以使用 Pandas 的 `isin()` 函数来检查。例如,你可以使用以下代码来检查要删除的标签是否存在于数据框中:
```
print(set(droplist).issubset(set(df1.index)))
```
这将返回一个布尔值,告诉你要删除的标签是否都存在于数据框中。如果不是,则需要进一步检查要删除的标签是否正确。
阅读全文