对数据框中对象类型的值进行切片

我有一个 DataFrame,其中列中的所有值都显示发布日期

https://img1.sycdn.imooc.com/65a4f8ef0001d48101670651.jpg

我需要删除附加文本才能得到以下结果:

从:

1879 [1879]

1879

我检查了数据类型并得到了以下结果:


Identifier               int64

Edition Statement       object

Place of Publication    object

Date of Publication     object

Publisher               object

Title                   object

Author                  object

Contributors            object

Flickr URL              object

dtype: object

最后,我尝试了以下方法,但它对我不起作用。


new_books = books['Date of Publication'].astype(object).apply(lambda x: x.str.slice(0, 3))

我究竟做错了什么?


aluckdog
浏览 34回答 1
1回答

幕布斯6054654

无需将调用包装在内部,apply只需对其Series本身进行操作即可:df["Date of Publication"].astype(str).str.slice(0, 4)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python