在AAA中将AAA BB CC等信用评级列转换为AAA = 1,BB = .75等数字类别?

问题描述 投票:-1回答:3

对于跨行的许多公司,我在数据框中有一个名为“CREDIT RATING”的列。我需要为AAA(DD)等评级分配数字类别,从1(AAA)到0(DDD)。有一个简单的快速方法来做到这一点,并基本上创建一个新的列,我得到.1的数字1-0?谢谢!

python categorical-data
3个回答
0
投票

您可以使用替换:

df['CREDIT RATING NUMERIC'] = df['CREDIT RATING'].replace({'AAA':1, ... , 'DDD':0})

0
投票

最简单的方法是简单地创建一个字典映射:

mymap = {"AAA":1.0, "AA":0.9, ... "DDD":0.0} 

然后将其应用于数据帧:

df["CREDIT MAPPING"] = df["CREDIT RATING"].replace(mymap)

0
投票

好吧,虽然没有什么可以解决的,但这里有点但是我们走了:

# First getting a ratings list acquired from wikipedia than setting into a dataframe to replicate your scenario

ratings = ['AAA' ,'AA1' ,'AA2' ,'AA3' ,'A1' ,'A2' ,'A3' ,'BAA1' ,'BAA2' ,'BAA3' ,'BA1' ,'BA2' ,'BA3' ,'B1' ,'B2' ,'B3' ,'CAA' ,'CA' ,'C' ,'C' ,'E' ,'WR' ,'UNSO' ,'SD' ,'NR']
df_credit_ratings = pd.DataFrame({'Ratings_id':ratings})

df_credit_ratings = pd.concat([df_credit_ratings,df_credit_ratings]) # just to replicate duplicate records

# The set() command get the unique values
unique_ratings = set(df_credit_ratings['Ratings_id'])
number_of_ratings = len(unique_ratings) # counting how many unique there are
number_of_ratings_by_tenth = number_of_ratings/10 # Because from 0 to 1 by 0.1 to 0.1 there are 10 positions.

# the numpy's arange fills values in between from a range (first two numbers) and by which decimals (third number)
dec = list(np.arange(0.0, number_of_ratings_by_tenth, 0.1))

在此之后,您需要将独特的评级与其权重混合:

df_ratings_unique = pd.DataFrame({'Ratings_id':list(unique_ratings)}) # list so it gets one value per row

编辑:正如托马斯在另一个答案的评论中所建议的那样,这种情况可能不适合你,因为它不会是评级的重要真实顺序。因此,您可能需要首先按顺序创建一个数据框,并且不需要排序。

df_ratings_unique.sort_values(by='Ratings_id', ascending=True, inplace=True) # sorting so it matches the order of our weigths above. 

恢复解决方案:

df_ratings_unique['Weigth'] = dec # adding the weigths to the DF

df_ratings_unique.set_index('Ratings_id', inplace=True) # setting the Rantings as index to map the values bellow

# now this is the magic, we're creating a new column at the original Dataframe and we'll map according to the `Ratings_id` by our unique dataframe
df_credit_ratings['Weigth'] = df_credit_ratings['Ratings_id'].map(df_ratings_unique.Weigth)
© www.soinside.com 2019 - 2024. All rights reserved.