用ix()方法用负索引切片大 pandas DataFrame [英] slicing pandas DataFrame with negative index with ix() method

查看:258
本文介绍了用ix()方法用负索引切片大 pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用负数索引时,DataFrame.ix()似乎不会对我想要的DataFrame进行分片。



我有一个DataFrame对象,想要分割最后2行。

 在[90]中:df = pd.DataFrame(np.random.randn(10,4))

在[91]中:df
输出[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

一种方法:

 在[92]中:df [-2:] 
输出[92]:
0 1 2 3
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704

另一种做法:

 在[93]中:df.ix [len(df)-2 :,] 
输出[93]:
0 1 2 3
8 0.110534 -0.234781 -0.819976 0.252080
9 1.798894 0.553394 -1.358335 1.278704

现在我想使用负号索引,但有问题:

 在[94]中:df.ix [-2: ] 
出[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

如何使用DataFrame.ix )正确吗谢谢。

解决方案

这是一个错误:

 在[1]中:df = pd.DataFrame(np.random.randn(10,4))

在[2]中:df
输出[2] :
0 1 2 3
0 -3.100926 -0.580586 -1.216032 0.425951
1 -0.264271 -1.091915 -0.602675 0.099971
2 -0.846290 1.363663 -0.382874 0.065783
3 - 0.099879 -0.679027 -0.708940 0.138728
4 -0.302597 0.753350 -0.112674 -1.253316
5 -0.213237 -0.467802 0.037350 0.369167
6 0.754915 -0.569134 -0.297824 -0.600527
7 0.644742 0.038862 0.216869 0.294149
8 0.101684 0.784329 0.218221 0.965897
9 -1.482837 -1.325625 1.008795 -0.150439

在[3]中:df.ix [-2:]
输出[3] :
0 1 2 3
0 -3.100926 -0.580586 -1.216032 0.425951
1 -0.264271 -1.091915 -0.602675 0.099971
2 -0.846290 1.363663 -0.382874 0.065783
3 - 0.099879 -0.679027 -0.708940 0.138728
4 -0.302597 0.753350 -0.112674 -1.253316
5 -0.213237 -0.467802 0.037350 0.369167
6 0.754915 -0.569134 -0.297824 -0.600527
7 0.644742 0.038862 0.216869 0.294149
8 0.101684 0.784329 0.218221 0.965897
9 -1.482837 -1.325625 1.008795 -0.150439

https://github.com/pydata/pandas/issues/2600



请注意 df [-2:] 将工作:

 在[4] :df [-2:] 
出[4]:
0 1 2 3
8 0.101684 0.784329 0.218221 0.965897
9 -1.482837 -1.325625 1.008795 -0.150439


DataFrame.ix() does not seem to slice the DataFrame that I want when negative indexing is used.

I have a DataFrame object and 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 it:

    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

Anther way to do it:

    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 having 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 do I use negative indexing with DataFrame.ix() correctly? Thanks.

解决方案

This is a bug:

In [1]: df = pd.DataFrame(np.random.randn(10, 4))

In [2]: df
Out[2]: 
          0         1         2         3
0 -3.100926 -0.580586 -1.216032  0.425951
1 -0.264271 -1.091915 -0.602675  0.099971
2 -0.846290  1.363663 -0.382874  0.065783
3 -0.099879 -0.679027 -0.708940  0.138728
4 -0.302597  0.753350 -0.112674 -1.253316
5 -0.213237 -0.467802  0.037350  0.369167
6  0.754915 -0.569134 -0.297824 -0.600527
7  0.644742  0.038862  0.216869  0.294149
8  0.101684  0.784329  0.218221  0.965897
9 -1.482837 -1.325625  1.008795 -0.150439

In [3]: df.ix[-2:]
Out[3]: 
          0         1         2         3
0 -3.100926 -0.580586 -1.216032  0.425951
1 -0.264271 -1.091915 -0.602675  0.099971
2 -0.846290  1.363663 -0.382874  0.065783
3 -0.099879 -0.679027 -0.708940  0.138728
4 -0.302597  0.753350 -0.112674 -1.253316
5 -0.213237 -0.467802  0.037350  0.369167
6  0.754915 -0.569134 -0.297824 -0.600527
7  0.644742  0.038862  0.216869  0.294149
8  0.101684  0.784329  0.218221  0.965897
9 -1.482837 -1.325625  1.008795 -0.150439

https://github.com/pydata/pandas/issues/2600

Note that df[-2:] will work:

In [4]: df[-2:]
Out[4]: 
          0         1         2         3
8  0.101684  0.784329  0.218221  0.965897
9 -1.482837 -1.325625  1.008795 -0.150439

这篇关于用ix()方法用负索引切片大 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆