Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

python - Why does pandas groupby cut give different form of output with single record input?

I'm having trouble with pandas (v0.11 or v0.12) groupby code returning a different structure of output when my input series has exactly one record. I use a fixed set of bins, and count the number of occurrences in each bucket, based on code similar to below.

s0 = pd.Series([1,2,1,3,1,4,1,2,1], name='foo')
s = s0
#s = s0[:0]
#s = s0[:1]
#s = s0[:2]
bins = pd.cut(s,[0,2,4])
s.groupby(bins).count()

For any length of input series (except 1), I get an output series with one record per bin along with a count for that bin, even if it's zero. Something like this:

foo
(0, 2]    7
(2, 4]    2
dtype: int64

But if the input series has length 1, I get output with a different structure, like this:

((0, 2])    1
dtype: int64

I'm wondering if this is related to the problem I often run into where a single-record slice of a dataframe turns into a series representing the row values, instead of a single-record dataframe. (Which you can avoid using df.ix[n:n] instead of df.ix[n].)

But I don't see how to work around it easily here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...