如何在熊猫的数据帧中对数据进行装箱

时间:2018-11-10 08:15:11

标签: python pandas dataframe statistics

我有一个时间序列数据,说机器读法如下(说)

df['machine_r'] = [1,2,1,5,3,4,5,1,2,3,4,5,7,8,1,2.....] 

如何像下面那样更改数据框

If data in dataframe <= 25 percentile, value = 0.25, 
if 25p < data <=50p  value =  0.50,
if 50p<data <= 75p, value = 0.75,
if data>75p , value = 1

我尝试过

p25 = df['machine_r'].quantile(0.25)  ## p25 is 25 percentile 
p50 = df['machine_r'].quantile(0.5)
p75 = df['machine_r'].quantile(0.8)
p100 = df['machine_r'].quantile(1)
bins = [-100,p25,p50,p75,p100]
labels = [0.25, 0.5,0.75,1]
df['machine_r'] = pd.cut(df['copper'], bins=bins,labels=labels)

,但它返回0、0.25、0.5、0.75、1作为分类值,但我需要将它们作为浮点数进行进一步分析。怎么办?

1 个答案:

答案 0 :(得分:1)

您可以按astype将其投射为浮动:

branch

更好的是像提到的qcut一样使用Sandeep Kadapa

df['new'] = pd.cut(df['machine_r'], bins=bins,labels=labels).astype(float)