简体   繁体   English

如何获得熊猫系列的最后一栏

[英]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. 我已经阅读了这篇文章 ,我正在使用DataFrame并获得一系列内容。

>>> 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 ? 如何获得b的最后一列?

Since pandas.Series is a 由于pandas.Series是一个

One-dimensional ndarray with axis labels 带轴标签的一维ndarray

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: PS:

For your specific task consider also collections package: 对于您的特定任务,请同时考虑collections包:

>>> 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: 问题是GroupBy.size输出是Series ,而Series没有列,因此可能仅获得最后一个值:

b.iloc[-1]

If use: 如果使用:

b.iloc[:,-1]

it return last column in Dataframe . 它返回Dataframe最后一列。

Here : means all rows and -1 in second position last column. 此处:表示所有行,最后一列第二个位置为-1

So if create DataFrame from Series : 因此,如果从Series创建DataFrame

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

it working like expected. 它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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