我正在使用pandas将一些数据转储到excel文件中。数据是字典格式的,我使用下面的代码来转储它。df1 = pd.DataFrame.from_dict(weights_dict, orient="index")
df1.columns = ['weights']
我得到以下输出| weights
------------------------
D1_sum | 8
U2_conv | 9
y1_maxpool | 10
y22_dropout | 11
我还想为第一列设置一个名称。我该怎么做?谢谢。
预期产量:Layers | weights
------------------------
D1_sum | 8
U2_conv | 9
y1_maxpool | 10
y22_dropout | 11
编辑:
使用后df.index.name = 'Layer'
我得到以下结果:| weights
------------------------
Layer |
------------------------
D1_sum | 8
U2_conv | 9
y1_maxpool | 10
y22_dropout | 11
使用重命名轴会产生以下错误:File "C:\ENV\p34\lib\site-packages\pandas\core\generic.py", line 573, in rename
result._data = result._data.rename_axis(f, axis=baxis, copy=copy)
File "C:\ENV\p34\lib\site-packages\pandas\core\internals.py", line 2233, in rename_axis
obj.set_axis(axis, _transform_index(self.axes[axis], mapper))
File "C:\ENV\p34\lib\site-packages\pandas\core\internals.py", line 3982, in _transform_index
items = [func(x) for x in index]
File "C:\ENV\p34\lib\site-packages\pandas\core\internals.py", line 3982, in
items = [func(x) for x in index]
TypeError: 'str' object is not callable