Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Since pandas.Series is a

One-dimensional ndarray with axis labels

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

b.tolist()

or, alternatively:

b.to_dict()

to keep both labels and frequencies.

P.S:

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]

Solution 2:

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.

Post a Comment for "How To Get Last Column Of Pandas Series"