我尝试了以下语句:
import pandas as pd
df = pd.DataFrame(index=[i for i in range(5)])
df.columns = ["res"+str(i) for i in range(5)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 5080, in __setattr__
return object.__setattr__(self, name, value)
File "pandas\_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 638, in _set_axis
self._data.set_axis(axis, labels)
File "C:\Python35\lib\site-packages\pandas\core\internals\managers.py", line 155, in set_axis
'values have {new} elements'.format(old=old_len, new=new_len))
ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements
不了解其原因。如果我有索引,那为什么不添加列呢?
答案 0 :(得分:3)
我认为问题是首先没有创建列DataFrame
,因此如果要分配列名称,则会失败。
解决方案是将参数columns
添加到DataFrame构造函数,索引应简化-仅分配range
:
df = pd.DataFrame(index= range(5), columns=["res"+str(i) for i in range(5)])
print (df)
res0 res1 res2 res3 res4
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
如果要在仅具有索引的DataFrame之后分配,可以使用DataFrame.reindex
:
df = pd.DataFrame(index=range(5))
df = df.reindex(["res"+str(i) for i in range(5)], axis=1)
print (df)
res0 res1 res2 res3 res4
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
答案 1 :(得分:1)
import pandas as pd
df = pd.DataFrame(index= range(5))
for i in range(5):
df['res'+str(i)]= '0'
print (df)
res0 res1 res2 res3 res4
0 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0