[英]Converting strings to numerical values
This image shows the dataframe I am working with这张图片显示了我正在使用的数据框
I want to do a few things:我想做几件事:
player_positions
to the count of unique values.将player_positions
转换为唯一值的计数。 So T. Barkhuizen, for instance, can play as a RM, RWB or ST and so would have a value of 3例如,T. Barkhuizen 可以扮演 RM、RWB 或 ST,因此其值为 3work_rate
to numerical values, with Low=1, Medium=2 and High=3 and sum the two values.将work_rate
转换为数值,其中 Low=1、Medium=2 和 High=3 并将两个值相加。 With Barkhuizen as an example again, his work_rate
value would be equal to 5 because he has High (3) and Medium (2)再次以 Barkhuizen 为例,他的work_rate
值将等于 5,因为他有 High (3) 和 Medium (2)player_traits
contains 'Injury Prone', convert to -1, otherwise convert to 1 (or possibly 0?)如果player_traits
包含 'Injury Prone',则转换为 -1,否则转换为 1(或可能为 0?)Thank you in advance先感谢您
You can do something like:您可以执行以下操作:
df['player_positions'] = df['player_positions'].apply(lambda x: len(set(x)))
For your work rate you could define a function that converts the values and returns the sum (if I understand how your data works, anyway).对于您的工作率,您可以定义一个函数来转换值并返回总和(如果我了解您的数据是如何工作的,无论如何)。 Example:例子:
def cnvWorkRate(rate):
rates = rate.split('/')
vals = {
'High':3,
'Medium':2,
'Low':1
}
return sum([vals[r] for r in rates])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.