简体   繁体   English

熊猫数据框列包含字符串和整数

[英]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.

相关问题 Pandas 如果列包含字符串,则写入第二个 dataframe - Pandas if column contains string then write to second dataframe Pandas Dataframe字符串列到连接Int - Pandas Dataframe String Column to Concatenated Int 将pandas dataframe列导入为字符串而不是int或float - Import pandas dataframe column as string not int or float 将 Pandas 数据框列作为字符串而不是 int 导入 - Import pandas dataframe column as string not int 在Pandas DataFrame中解析列,其中一列包含嵌套的JSON字符串 - Parsing Column in Pandas DataFrame with one column that contains a nested JSON string pandas dataframe 检查列是否包含存在于另一列中的字符串 - pandas dataframe check if column contains string that exists in another column 在列上减去列,避免在熊猫数据框中包含字符串 - Subtract column over column avoiding a string contains in pandas Dataframe 检查字符串是否包含pandas dataframe中同一列的子字符串 - check if string contains sub string from the same column in pandas dataframe 根据条件(包含字符串)替换 Pandas DataFrame 列中的值 - Replace value in Pandas DataFrame column, based on a condition (contains a string) 如果列包含任何指定的部分字符串,Pandas Dataframe保留行 - Pandas Dataframe Keep Row If Column Contains Any Designated Partial String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM