DataFrame.ix () doesn't seem to break the DataFrame I want when using negative indexing.
I have a DataFrame object and you want to slice the last 2 rows.
In [90]: df = pd.DataFrame(np.random.randn(10, 4)) In [91]: df Out[91]: 0 1 2 3 0 1.985922 0.664665 -2.800102 1.695480 1 0.580509 0.782473 1.032970 1.559917 2 0.584387 1.798743 0.095950 0.071999 3 1.956221 0.075530 -0.391008 1.692585 4 -0.644979 -1.959265 0.749394 -0.437995 5 -1.204964 0.653912 -1.426602 2.409855 6 1.178886 2.177259 -0.165106 1.145952 7 1.410595 -0.761426 -1.280866 0.609122 8 0.110534 -0.234781 -0.819976 0.252080 9 1.798894 0.553394 -1.358335 1.278704
One way to do this:
In [92]: df[-2:] Out[92]: 0 1 2 3 8 0.110534 -0.234781 -0.819976 0.252080 9 1.798894 0.553394 -1.358335 1.278704
An inquisitive way to do this:
In [93]: df.ix[len(df)-2:, :] Out[93]: 0 1 2 3 8 0.110534 -0.234781 -0.819976 0.252080 9 1.798894 0.553394 -1.358335 1.278704
Now I want to use negative indexing, but I have a problem:
In [94]: df.ix[-2:, :] Out[94]: 0 1 2 3 0 1.985922 0.664665 -2.800102 1.695480 1 0.580509 0.782473 1.032970 1.559917 2 0.584387 1.798743 0.095950 0.071999 3 1.956221 0.075530 -0.391008 1.692585 4 -0.644979 -1.959265 0.749394 -0.437995 5 -1.204964 0.653912 -1.426602 2.409855 6 1.178886 2.177259 -0.165106 1.145952 7 1.410595 -0.761426 -1.280866 0.609122 8 0.110534 -0.234781 -0.819976 0.252080 9 1.798894 0.553394 -1.358335 1.278704
How to use negative indexing correctly with DataFrame.ix ()? Thank.
pandas slice indexing dataframe
Julia He Dec 26 2018-12-12T00: 00Z
source share