ValueError:索引必须是单调递增或递减的,包括index、column、ffill在同一时刻

2024-05-29 02:11:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的pandas版本是'0.20.1',python3

尽管有两个问题: question1question2 一直被问到同样的错误,而我发现这两个问题与我的情况不一样。在

数据来源于《Python for data analysis》一书,第123-124页。 当我运行以下代码时

frame = DataFrame(np.arange(9).reshape((3, 3)), index=['a', 'c', 'd'],
                  columns=['Ohio', 'Texas', 'California'])
states = ['Texas', 'Utah', 'California']
frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill',columns=states)

it报告错误

^{pr2}$

虽然我尝试了以下两个表达式,但它们运行成功:

frame.reindex(index=['a', 'b', 'c', 'd'], columns=states)

或者

frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill')

**********************更新*************

我试过这个密码

frame3=frame.reindex(index=['a', 'b', 'c', 'd'], method='ffill').reindex(columns=states)

然后它返回与书本相同的结果。在

Out[92]:
Texas Utah California
a 1 NaN 2
b 1 NaN 2
c 4 NaN 5
d 7 NaN 8

Tags: columns版本pandasindex错误nanframemethod

热门问题