熊猫 - 如何切片value_counts?

时间:2016-05-30 09:35:43

标签: python pandas

我想切片pandas value_counts():

    GeoPoint geoPoint = new GeoPoint(mLatitude, mLongitude);
    IMapController mapController = mMapView.getController();
    mapController.setZoom(5);
    mapController.setCenter(geoPoint);

    Overlay overlay = new Overlay(getActivity()) {

        ItemizedIconOverlay<OverlayItem> items = null;

        @Override
        protected void draw(Canvas c, MapView osmv, boolean shadow) {

        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {
            Projection proj = mapView.getProjection();
            GeoPoint loc = (GeoPoint) proj.fromPixels((int)e.getX(), (int)e.getY());
            double longitude = loc.getLongitude();
            double latitude = loc.getLatitude();
            mLatitude = latitude;
            mLongitude = longitude;

            ArrayList<OverlayItem> markers = new ArrayList<>();
            OverlayItem item = new OverlayItem("", "", new GeoPoint(latitude, longitude));
            item.setMarker(ContextCompat.getDrawable(getActivity(), R.drawable.ic_maps_marker_large));
            markers.add(item);

            if (items == null) {
                items = new ItemizedIconOverlay<>(getActivity(), markers, null);
                mMapView.getOverlays().add(items);
                mMapView.invalidate();
            } else {
                mMapView.getOverlays().remove(items);
                mMapView.invalidate();
                items = new ItemizedIconOverlay<>(getActivity(), markers, null);
                mMapView.getOverlays().add(items);
            }
            return true;
        }

    };

    mMapView.getOverlays().add(overlay);

但是我收到了一个错误:

>sur_perimetre[col].value_counts()
44341006.0    610
14231009.0    441
12131001.0    382
12222009.0    364
12142001.0    354

与ix相同:

> sur_perimetre[col].value_counts()[:5]
KeyError: 5.0

你会如何处理?

修改

也许:

> sur_perimetre[col].value_counts().ix[:5]
KeyError: 5.0

1 个答案:

答案 0 :(得分:3)

方法1:

您需要观察value_counts()返回一个Series对象。您可以像处理任何其他系列一样处理它并获取值。您甚至可以构建一个新的数据帧。

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([1,2,3,3,4,5], columns=['C1'])

In [3]: vc = df.C1.value_counts()

In [4]: type(vc)
Out[4]: pandas.core.series.Series

In [5]: vc.values
Out[5]: array([2, 1, 1, 1, 1])

In [6]: vc.values[:2]
Out[6]: array([2, 1])

In [7]: vc.index.values
Out[7]: array([3, 5, 4, 2, 1])

In [8]: df2 = pd.DataFrame({'value':vc.index, 'count':vc.values})

In [8]: df2
Out[8]: 
   count  value
0      2      3
1      1      5
2      1      4
3      1      2
4      1      1

方法2:

然后,我试图重新生成你提到的错误。但是,在DF中使用单个列时,我没有像你提到的那样用相同的符号表示任何错误。

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([1,2,3,3,4,5], columns=['C1'])

In [3]: df['C1'].value_counts()[:3]
Out[3]: 
3    2
5    1
4    1
Name: C1, dtype: int64      

In [4]: df.C1.value_counts()[:5]
Out[4]: 
3    2
5    1
4    1
2    1
1    1
Name: C1, dtype: int64

In [5]: pd.__version__
Out[5]: u'0.17.1'

希望它有所帮助!