[英]pandas dataframe column contains string and int
My data-frame age column looks like this我的数据框年龄列看起来像这样
20 or younger =14 20岁以下=14
61 or older =45 61岁以上=45
56-60 = 34 56-60 = 34
31-35 =30 31-35 =30
56 or older =31 56 岁或以上 =31
21-25 =23 21-25 =23
26 30 =56 26 30 =56
31 35 =44 31 35 =44
36 40 =32 36 40 =32
21 25 =26 21 25 =26
26-30 =14 26-30 =14
46 50 =14 46 50 =14
36-40 =15 36-40 =15
46-50 =33 46-50 =33
41 45 =24 41 45 =24
41-45 =29 41-45 =29
51-55 =35 51-55 =35
so i wrote this function to categorize it better but i got this typeerror message that says '<' not supported between instance of str and int所以我写了这个函数来更好地对其进行分类,但是我收到了这个类型错误消息,上面写着'<'在 str 和 int 的实例之间不支持
def age_buckets(x): def age_buckets(x):
if x < 30:
return '18-29'
elif x < 40:
return '30-39'
elif x < 50:
return '40-49'
elif x < 60:
return '50-59'
elif x < 70:
return '60-69'
elif x >=70:
return '70+'
else: return 'other'
Here is a link to what i am doing https://deepnote.com/workspace/eddie-abfa350f-f15e-43fe-8960-fab53a2def2e/project/Welcome-e6ac66b9-19f2-4973-bbc2-7adfda9366f3/%2FReasons%20for%20resignation%20analysis.ipynb这是我正在做的事情的链接https://deepnote.com/workspace/eddie-abfa350f-f15e-43fe-8960-fab53a2def2e/project/Welcome-e6ac66b9-19f2-4973-bbc2-7adfda9366f3/%2FReasons%20for% 20resignation%20analysis.ipynb
You can't compare a string of characters with the <
check.您不能将字符串与
<
检查进行比较。 It doesn't associate that string with a number.它不会将该字符串与数字相关联。 That error says that the incoming
x
value is a string
.该错误表示传入的
x
值是string
。 Therefore, in order to do this, x
must be a number.因此,为了做到这一点,
x
必须是一个数字。 If it is in-fact an int
, you can cast it with the int()
function.如果它实际上是
int
,则可以使用int()
函数对其进行转换。 Such as int(x) < 30
...比如
int(x) < 30
...
What would be better is that you pass age_buckets
an int
rather than a string
.更好的是您将
age_buckets
传递给int
而不是string
。 So when you call it just do age_buckets(int(x))
rather than just age_buckets(x)
因此,当您调用它时,只需执行
age_buckets(int(x))
而不仅仅是age_buckets(x)
Please see : https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html请参阅: https ://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html
So when you do combined['age'] = combined['age'].apply(age_buckets(int(x)))
you actually need to do combined['age'] = combined['age'].apply(age_buckets,1))
所以当你做
combined['age'] = combined['age'].apply(age_buckets(int(x)))
你实际上需要做 combine combined['age'] = combined['age'].apply(age_buckets,1))
See if :看看 :
def age_buckets(y):
x = int(y)
if x < 30:
...
works作品
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.