简体   繁体   English

Pandas 按行 Q-Cut

[英]Pandas Q-Cut by Row

I have a dataframe named "df" with a datetime index and four columns:我有一个名为“df”的数据框,带有一个日期时间索引和四列:

            A    B   C   D
1/1/2020    0.1 0.3 0.2 0.2
1/2/2020    0.3 0.1 0.3 0.3
1/3/2020    0.2 0.2 0.3 0.1
1/4/2020    0.1 0.1 0.1 0.3

I would like to divide the data into 4 "discretized" quantiles.我想将数据分成 4 个“离散化”分位数。

If I wanted to do this for the column "A", all I would need to do is to use Pandas's q-cut function as below:如果我想对“A”列执行此操作,我需要做的就是使用 Pandas 的 q-cut 函数,如下所示:

df["A"] = pd.qcut(df["A"], 4)

However, the problem is I would like to create quantiles for each date, ie to divide the data into 4 quintiles for each row (NOT column).但是,问题是我想为每个日期创建分位数,即将数据分成每行(不是列)的 4 个五分位数。 How would I do this?我该怎么做?

您可以将.applyaxis=1参数一起使用:

df.apply(lambda x: pd.qcut(x, 4, duplicates='drop'), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM