获取相应的中位数指数 [英] Get corresponding index of median

查看:134
本文介绍了获取相应的中位数指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一栏的pandas数据框,我想知道中位数的索引。也就是说,我以这种方式确定中位数:

I have a pandas dataframe with one column and I would like to know the index of the median. That is, I determine the median this way:

df.median()

df.median()

这给了我中位数值,但我想知道该行的索引。有可能确定吗?对于长度不均匀的列表,我可以搜索具有该值的索引,但是对于均匀列表长度,这将无法正常工作。有人可以帮忙吗?

This gives me the median value, but I would like to know the index of that row. Is it possible to determine this? For a list with uneven length I could search for the index with that value but for even list lengths this is not going to work. Can someone help?

这个问题是在另一篇文章中提出的,答案基本上是搜索与中位数相同的行。但是,就像我说的那样,这对于长度相等的列表将不起作用。

This question was asked in another post, where the answer was basically to search for rows which have the same value as the median. But like I said, that will not work for a list of even length.

下面是一个最小示例(我在下面提供了Wen的建议):

Below is a Min Example (I have included the suggestion by Wen below):

df = pd.DataFrame(np.random.randn(6, 1), columns=list('A'))
df.median()
df.loc[df[0]==df[0].median()]

Out[120]: 
Empty DataFrame
Columns: [0]
Index: []


推荐答案

您可以将Wen的答案用于奇数长度的数据帧。

You can use Wen's answer for dataframes of odd length.

对于偶数长度的数据帧,这个问题真的没有道理。如您所指出的,数据框中不存在中位数。但是,您可以按感兴趣的列对数据框进行排序,然后找到两个中位数值的索引。

For dataframes of even length, the question does not really make sense. As you have pointed out the median does not exist in the dataframe. However, you can sort the dataframe by your column of interest and then find the indices for the two "median" values.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 1), columns=list('A'))
df.median()

df.loc[df['A']==df['A'].median()]

df.sort_values(by='A', inplace=True)

df[df['A'] > df['A'].median()].iloc[0]
df[df['A'] < df['A'].median()].iloc[-1]

这篇关于获取相应的中位数指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆