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
672 views
in Technique[技术] by (71.8m points)

python - How I do find median using pandas on a dataset?

I have dataframe data which has 3 columns - Date, segment and metric. I am doing the following:

data = pandas.read_csv("Filename.csv")
ave = data.groupby('Segment').mean() #works
ave = data.groupby('Segment').median() #gives error
ave['median'] = data.groupby('Segment').median()

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/pandas/core/frame.py", line 1453, in __setitem__
    self._set_item(key, value)
  File "/usr/lib/pymodules/python2.7/pandas/core/frame.py", line 1488, in _set_item
    NDFrame._set_item(self, key, value)
  File "/usr/lib/pymodules/python2.7/pandas/core/generic.py", line 301, in _set_item
    self._data.set(key, value)
  File "/usr/lib/pymodules/python2.7/pandas/core/internals.py", line 616, in set
    assert(value.shape[1:] == self.shape[1:])
AssertionError
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

What error do you get with?

ave = data.groupby('Segment').median()

I think that should work, maybe there's something in your data causing the error, like nan's, im just guessing. You could try applying your own median function to see if you can work around the cause of the error, something like:

def mymed(group):
    return np.median(group.dropna())

ave = data.groupby('segment')['Metric'].apply(mymed)

It would be easier if you could provide some sample data which replicates the error.

Here is a different approach, you can add the median back to your original dataframe, the median for the metric column becomes:

data['metric_median'] = data.groupby('Segment')['Metric'].transform('median')

Wether its useful to have the median of the group attached to each datapoint depends a bit what you want to do afterwards.


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

...