简体   繁体   中英

how to get last column of pandas series

I am trying to count frequencies of an array. I've read this post , I am using DataFrame and get a series.

>>> a = np.array([1, 1, 5, 0, 1, 2, 2, 0, 1, 4])
>>> df = pd.DataFrame(a, columns=['a'])
>>> b = df.groupby('a').size()
>>> b
a
0    2
1    4
2    2
4    1
5    1
dtype: int64
>>> b.iloc[:,-1]

when i try to get the last column, i got this error.

Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/Users/pan/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1472, in __getitem__
    return self._getitem_tuple(key)   File "/Users/pan/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2013, in _getitem_tuple
    self._has_valid_tuple(tup)   File "/Users/pan/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 220, in _has_valid_tuple
    raise IndexingError('Too many indexers') pandas.core.indexing.IndexingError: Too many indexers

how to get the last column of b ?

Since pandas.Series is a

One-dimensional ndarray with axis labels

If you want to get just the frequencies column, ie the values of your series, use:

b.tolist()

or, alternatively:

b.to_dict()

to keep both labels and frequencies.

PS:

For your specific task consider also collections package:

>>> from collections import Counter
>>> a = [1, 1, 5, 0, 1, 2, 2, 0, 1, 4]
>>> c = Counter(a)
>>> list(c.values())
[2, 4, 2, 1, 1]

Problem is output of GroupBy.size is Series , and Series have no columns, so is possible get last value only:

b.iloc[-1]

If use:

b.iloc[:,-1]

it return last column in Dataframe .

Here : means all rows and -1 in second position last column.

So if create DataFrame from Series :

b1 = df.groupby('a').size().reset_index(name='count')

it working like expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM