获取 Typerror 尝试将对象值转换为整数或日期时间值年份

时间:2021-03-12 11:29:27

标签: pandas dataframe

这是数据框

enter image description here

movie_df["year"] 列是从标题列中提取值创建的:

movie_df["year"] = movie_df["title"].str.extract("(\d+)")

代码行

enter image description here

错误

enter image description here

1 个答案:

答案 0 :(得分:0)

在python中int是一个将字符串转换为整数的函数。因此,当您传递它时,您实际上是在传递错误描述的方法。从 pandas 的文档来看,它看起来像 .astype 需要一个字符串作为参数。例如:

movie_df["year"].astype("int32")

文档:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.astype.html