我的题目是从一个txt中读取一些数据,经过某个函数,得到想要的结果:
三组数据分别存放在了input_number、target_number、output_number三个list中,并且长度均为3
但是创建dataframe并且赋值后,出现了如下错误:
import pandas as pd
contents=["输入","target","输出"]
df=pd.DataFrame(columns=contents)
df.iloc[:,0]=input_number
df.iloc[:,1]=target_number
df.iloc[:,2]=output_number
错误:
ValueError Traceback (most recent call last)
<ipython-input-81-e33a778f90ec> in <module>()
23 df.iloc[:,0]=input_number
24 df.iloc[:,1]=target_number
---> 25 df.iloc[:,2]=output_number
D:\Anaconda\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
192 key = com._apply_if_callable(key, self.obj)
193 indexer = self._get_setitem_indexer(key)
--> 194 self._setitem_with_indexer(indexer, value)
195
196 def _has_valid_type(self, k, axis):
D:\Anaconda\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value)
581 value = np.array(value, dtype=object)
582 if len(labels) != value.shape[1]:
--> 583 raise ValueError('Must have equal len keys and value '
584 'when setting with an ndarray')
585
ValueError: Must have equal len keys and value when setting with an ndarray
然后我尝试了将最后一行代码放在第二位,或者第一位:
import pandas as pd
contents=["输入","target","输出"]
df=pd.DataFrame(columns=contents)
df.iloc[:,2]=output_number
df.iloc[:,0]=input_number
df.iloc[:,1]=target_number
import pandas as pd
contents=["输入","target","输出"]
df=pd.DataFrame(columns=contents)
df.iloc[:,0]=input_number
df.iloc[:,2]=output_number
df.iloc[:,1]=target_number
都是可以执行成功的:
请问这是什么原因呢?找了一下发现有类似的,都是迭代之类的问题,但是我这个好像不是啊....
谢谢各位!!