python对某一列数据求占比_如何使用python计算相对于另一列的数据列的百分位数排名...

这是一个解决方案.

对训练数据进行排序.然后在验证数据上使用searchsorted.

import pandas as pd

import numpy as np

# Generate Dummy Data

df_train = pd.DataFrame({'Values': 1000*np.random.rand(15712)})

#Sort Data

df_train = df_train.sort_values('Values')

# Calculating Rank and Rank_Pct for demo purposes

#but note that it is not needed for the solution

# The ranking of the validation data below does not depend on this

df_train['Rank'] = df_train.rank()

df_train['Rank_Pct']= df_train.Values.rank(pct=True)

# Demonstrate how Rank Percentile is calculated

# This gives the same value as .rank(pct=True)

pct_increment = 1./len(df_train)

df_train['Rank_Pct_Manual'] = df_train.Rank*pct_increment

df_train.head()

Values Rank Rank_Pct Rank_Pct_Manual

2724 0.006174 1.0 0.000064 0.000064

3582 0.016264 2.0 0.000127 0.000127

5534 0.095691 3.0 0.000191 0.000191

944 0.141442 4.0 0.000255 0.000255

7566 0.161766 5.0 0.000318 0.000318

现在使用searchsorted来获取验证数据的Rank_Pct

# Generate Dummy Validation Data

df_validation = pd.DataFrame({'Values': 1000*np.random.rand(1000)})

# Note searchsorted returns array index.

# In sorted list rank is the same as the array index +1

df_validation['Rank_Pct'] = (1 + df_train.Values.searchsorted(df_validation.Values))*pct_increment

以下是最终df_validation数据帧的前几行:

print df_validation.head()

Values Rank_Pct

0 307.378334 0.304290

1 744.247034 0.744208

2 669.223821 0.670825

3 149.797030 0.145621

4 317.742713 0.314218

你可能感兴趣的:(python对某一列数据求占比)