尝试将文本列表转换为小写,但它会将所有内容都转换为 NaN
Trying to convert text List to lower case but it turns everything to 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
..
任何人都可以帮助我了解我做错了什么以及我可能如何解决它吗?感谢您的宝贵时间!!!
您尝试使用字符串方法转换包含列表的列。所以你需要创建一个简单的函数,例如:
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
结果是:
col
0 [titi, toto, tutu]
1 [tata, toto, tutu]
简单的方法是对每个列表使用 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
我目前正在尝试处理文本数据,我在这方面还比较陌生。我尝试使用的列是 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
..
任何人都可以帮助我了解我做错了什么以及我可能如何解决它吗?感谢您的宝贵时间!!!
您尝试使用字符串方法转换包含列表的列。所以你需要创建一个简单的函数,例如:
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
结果是:
col
0 [titi, toto, tutu]
1 [tata, toto, tutu]
简单的方法是对每个列表使用 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