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

Slicing the values of object type in data frame

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

我需要删除额外的文本才能得到以下结果:

发件人:

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))

我做错了什么?

无需将调用包装在 apply 内,您只需对 Series 本身进行操作:

df["Date of Publication"].astype(str).str.slice(0, 4)