2

the pandas version I use is '0.20.1', python 3

Though there are two questions: question1 , question2 have been asked with same error, while I found those two questions are not the same situation with me.

The data is source from the book "Python for data analysis", page 123-124. when I run the following codes,

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 report error

ValueError: index must be monotonic increasing or decreasing

while I have tried following two expression, they run successfully:

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

or

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

***********************update***************

I tried this code,

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

then it return the same result as the book.

Out[92]:
Texas Utah California
a 1 NaN 2
b 1 NaN 2
c 4 NaN 5
d 7 NaN 8
Renke
  • 452
  • 6
  • 22

1 Answers1

-1

Because the columns are also reindexed, which are not monotonic increasing or decreasing.