简体   繁体   English

使用Python从数据框中删除仅具有单个单词的列值

[英]Remove Column values having Single word only from a Data Frame using Python

I am new to programming. 我是编程新手。 I have a DataFrame shown in as below: 我有一个DataFrame如下所示:

Col-2               Col-3
have a account      A
account summary     B
Cancel              C
Both                D
Update credit card  E
Block Credit card   F

I need my output as: 我需要我的输出为:

Col-2               Col-3
have a account      A
account summary     B
Update credit card  E
Block Credit card   F

Means I need those values where Col-2 is having more than one word. 意味着我需要Col-2具有多个单词的那些值。 Single word present in Col-2 should be removed. Col-2存在的单个单词应被删除。 Both and Cancel are single words, that's why those rows have been removed from the output. BothCancel是简单的词,这就是为什么那些行已经从输出中删除。

A list comprehension here can be faster than pandas str methods. 这里的列表理解可能比pandas str方法要快。 Use it to get the length of each value in Col-2 after you split it, and index your dataframe by whether that length is greater than 1: 拆分后,使用它来获取Col-2中每个值的长度,并根据该长度是否大于1来索引数据帧:

>>> df[[len(i.split())>1 for i in df['Col-2'].values]]

                Col-2 Col-3
0      have a account     A
1     account summary     B
4  Update credit card     E
5   Block Credit card     F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM