【发布时间】:2021-07-07 16:45:39
【问题描述】:
我有一个 Pandas 系列,其中包含如下字符串列表:
series_of_list.head()
0 ['hello','there','my','name']
1 ['hello','hi','my','name']
2 ['hello','howdy','my','name']
3 ['hello','mate','my','name']
4 ['hello','hello','my','name']
type(series_of_list)
pandas.core.series.Series
我想只保留列表中的第一个条目,如下所示:
series_of_list.head()
0 ['hello','there']
1 ['hello','hi']
2 ['hello','howdy']
3 ['hello','mate']
4 ['hello','hello']
我尝试过对其进行切片,series_of_list=series_of_list[:2],但这样做只会返回该系列的前两个索引...
series_of_list.head()
0 ['hello','there','my','name']
1 ['hello','hi','my','name']
我也尝试过.drop 和其他切片,但结果不是我想要的。
如何只保留整个熊猫系列列表的前两项?
谢谢!
【问题讨论】:
-
series_of_list.str[:2]?