无法将系列转换为< class'int'`>

编程入门 行业动态 更新时间:2024-10-28 06:30:50
本文介绍了无法将系列转换为< class'int'`>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一组带有年龄"列的数据.我要删除所有年龄大于90岁且小于1856年的行.

I have a set of data with an Age column. I want to remove all the rows that are aged more than 90 and less than 1856.

这是df的负责人:

这是我尝试的:

推荐答案

您的错误是第2行.df['intage'] = int(df['age'])无效,您无法将pandas系列传递给int函数.

Your error is line 2. df['intage'] = int(df['age']) is not valid, you can't pass a pandas series to the int function.

如果df ['age']是对象dtype,则需要使用astype.

You need to use astype if df['age'] is object dtype.

df['intage'] = df['age'].astype(int)

或者因为要减去两个日期,所以需要使用带days属性的dt访问器来获取天数为整数

Or since you are subtracting two dates, you need to use dt accessor with the days attribute to get the number of days as an integer

df['intage'] = df['age'].dt.days

更多推荐

无法将系列转换为< class'int'`>

本文发布于:2023-11-07 06:59:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   系列   amp   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!