Python/Pandas中沿水平行的百分位数

2024-09-29 01:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在一个数据帧中,沿行计算不同的百分位数到一列。例如:

df['P90'] = df[['col1','col2','col3','col4','col5']].apply(quantile(0.9), axis=1)
df['P50'] = df[['col1','col2','col3','col4','col5']].apply(quantile(0.5), axis=1)

我有以下数据帧:

ID      2019/31 2019/32 2019/33 2019/34 2019/35 2019/36 2019/37 2019/38 2019/39 2019/40
258101  67000                                   
258102  56750   19105   35990   41250   44425   51275   1071    8125    16375
258103  8528    6853    3291            3000    5640    11248   
258104          27532   19523   12092   7933    8675    435     1045    5115    1450
258105          40000                   285500  16500

我需要以下格式的输出:

ID      2019/31 2019/32 2019/33 2019/34 2019/35 2019/36 2019/37 2019/38 2019/39 2019/40 P_50    P_90 
258101  67000                                                                           x1      x2
258102  56750   19105   35990   41250   44425   51275   1071    8125    16375           x3      x4
258103  8528    6853    3291            3000    5640    11248                           x5      x6
258104          27532   19523   12092   7933    8675    435     1045    5115    1450    x7      x8
258105          40000                   285500  16500                                   x9      x10

我尝试了以下方法:

cols = ['2019/31', '2019/32', '2019/33', '2019/34', '2019/35', '2019/36', '2019/37', '2019/38', '2019/39', '2019/40']
df['P_50'] = df[cols].apply(np.median, axis=1)
df['P_50'] = df[cols].apply(np.quantile(0.5), axis=1)

perc99 = np.vectorize(lambda x: np.percentile(x, 50))
df['P_50'] = perc99(df[cols].values)

所有这些都没有提供所需的输出


Tags: 数据iddfnpcol2col3col1apply
2条回答

我认为,这将起作用:

df['P_50'] = np.median(df.values, axis=1)
df['P_90'] = np.quantile(df.values, 0.9, axis=1)

我把你的df从帖子上删掉了。这就是你想做的吗

df['P_50'] = df.iloc[:,1:-1].median(axis=1)
df['P_50'] = df.iloc[:,1:-1].quantile(0.5, axis=1)

这不包括ID列

相关问题 更多 >