首页 > 解决方案 > 如果熊猫列中只有单词,如何在熊猫数据框中删除一行

问题描述

如果列中只有一个单词,我们如何在 pandas 数据框中删除一整行

例子:

'the cat likes mice',
'the dog likes the cat',
'dog'

返回

'the cat likes mice',
'the dog likes the cat'

标签: pythonpandas

解决方案


如何使用该pd.Series.str.contains方法查找空格:

df = pd.DataFrame({'items': ['the cat likes mice',
                             'the dog likes the cat',
                             'dog']})

df = df[df['items'].str.contains(' ')]

推荐阅读