试图将文本列表转换为小写,但它会将所有内容都转换为 NaN

我目前正在尝试使用文本数据,我在这方面相对较新。我正在尝试使用的列是 cast 列,如下所示:


0    [Sam Worthington, Zoe Saldana, Sigourney Weave...

1    [Johnny Depp, Orlando Bloom, Keira Knightley, ...

2    [Daniel Craig, Christoph Waltz, Léa Seydoux, R...

3    [Christian Bale, Michael Caine, Gary Oldman, A...

4    [Taylor Kitsch, Lynn Collins, Samantha Morton,...

Name: cast, dtype: object 

我想要的是降低所有大写字母。但是,当我尝试这样做时,它会将所有内容转换为 NaN 值。


这是我所做的简单的事情:


data.cast=data.cast.str.lower()

这是输出:


0      NaN

1      NaN

2      NaN

3      NaN

4      NaN

5      NaN

6      NaN

7      NaN

8      NaN

9      NaN

10     NaN

11     NaN

12     NaN

13     NaN

14     NaN

15     NaN

16     NaN

17     NaN

18     NaN

19     NaN

20     NaN

21     NaN

22     NaN

23     NaN

24     NaN

25     NaN

26     NaN

27     NaN

28     NaN

29     NaN

        ..

谁能帮助我了解我做错了什么以及如何解决它?感谢您的时间!!!


一只萌萌小番薯
浏览 97回答 2
2回答

森林海

您尝试使用字符串方法转换包含列表的列。所以你需要创建一个简单的函数,例如:def lower(l):    return [x.lower() for x in l]并使用地图删除大写:data = pd.DataFrame([{'col':['Titi','Toto','Tutu']},{'col':['Tata','Toto','Tutu']}])data.col = data.col.map(lower)data结果是:    col0   [titi, toto, tutu]1   [tata, toto, tutu]

LEATH

简单的方法是使用 listcomp 和map str.lower每个列表:s[:] = [list(map(str.lower, x)) for x in  s]Out[915]:0    [ zoe saldana,  sigourney weave, sam worthington]1      [ orlando bloom, johnny depp,  keira knightley]2       [daniel craig,  christoph waltz,  léa seydoux]3       [ michael caine,  gary oldman, christian bale]4     [ samantha morton, taylor kitsch,  lynn collins]dtype: object
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python