Code Sample, a copy-pastable example if possible
df.columns Index([u'a', u'b', u'c', u'd'], dtype='object')
Dont do this, looks like a bug.
df.columns.drop(["b"]).insert(-1, 'b') Index([u'a', u'c', u'b', u'd'], dtype='object')
df.columns.drop(["b"]).insert(-1, 'x') Index([u'a', u'c', u'x', u'd'], dtype='object') WORK Around:
df.columns.drop(["b"]).insert(99999, 'b') Index([u'a', u'c', u'd', u'b'], dtype='object')
Expected Output
df.columns.drop(["b"]).insert(-1, 'b') Index([u'a', u'c', u'd', u'b'], dtype='object')
output of pd.show_versions()
pandas 18.1
http://stackoverflow.com/questions/38601841/what-is-the-quickest-way-to-ensure-a-specific-column-is-last-or-first-in-a-dat/38602601#38602601
INSTALLED VERSIONS
commit: None python: 2.7.9.final.0 python-bits: 64 OS: Linux OS-release: 4.1.12-1-default machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.utf8
pandas: 0.18.1 nose: 1.3.7 pip: 8.1.2 setuptools: 24.0.2 Cython: None numpy: 1.11.1 scipy: 0.17.0 statsmodels: None xarray: None IPython: 4.1.1 sphinx: 1.4.4 patsy: 0.4.0 dateutil: 2.5.3 pytz: 2016.4 blosc: 1.2.8 bottleneck: None tables: None numexpr: 2.4.6 matplotlib: 1.5.1 openpyxl: None xlrd: 0.9.4 xlwt: 1.0.0 xlsxwriter: 0.8.4 lxml: 3.2.3 bs4: 4.3.2 html5lib: 0.9999999 httplib2: 0.9.1 apiclient: 1.4.2 sqlalchemy: 1.0.11 pymysql: None psycopg2: None jinja2: 2.8 boto: 2.38.0 pandas_datareader: 0.2.1
Comment From: sinhrks
It's based on np.insert
.
arr = np.array(["a", "b", "c", "d"])
np.insert(arr, -1, "x")
# array(['a', 'b', 'c', 'x', 'd'], dtype='<U1')
you can do:
np.insert(arr, len(arr), 'x')
# array(['a', 'b', 'c', 'd', 'x'], dtype='<U1')
Comment From: BMeridian
No. why does .insert(number, 'b') .. number works! the fact that -1 adds to list -- with out throwing an error but insert in wrong sequence should qualify as a bug. python list behave this way. You adding incontinence to expect output. THIS IS A BUG
Comment From: TomAugspurger
python list behave this way
Could you show an example of this?
In [16]: columns = pd.Index(['a', 'b', 'c', 'd'])
In [17]: l = columns.tolist()
In [18]: l
Out[18]: ['a', 'b', 'c', 'd']
In [19]: l.pop(1)
Out[19]: 'b'
In [20]: l
Out[20]: ['a', 'c', 'd']
In [21]: l.insert(-1, 'b')
In [22]: l
Out[22]: ['a', 'c', 'b', 'd']
In [23]: columns.drop('b').insert(-1, 'b')
Out[23]: Index(['a', 'c', 'b', 'd'], dtype='object')
As you can see the python and pandas version are the same. Looking at the docs for list.insert
, the value gets inserted before index
passed to .insert
.
Comment From: BMeridian
Ok, its weird.
LL= list('12312312312')
LL.pop(-1)
"2"
LL= list('12312312312')
LL[-1:]
['2']
But insert works differently.
Comment From: jreback
@SlyOne
I think @TomAugspurger example is pretty clear. If you want to add a documentation example to some of the Index methods, by all means.
Comment From: BMeridian
I was looking could find it, yet. What are the methods hanging off
'df.columns'.
Its an index. Where in the docs are 'index' methods kept.
Comment From: jreback
https://github.com/pydata/pandas/blob/master/pandas/indexes/base.py