gpt4 book ai didi

python - 输出一列字数大于 3 的所有行

转载 作者:行者123 更新时间:2023-12-03 14:50:53 25 4
gpt4 key购买 nike

我有这个虚拟 df:

columns = ['answer', 'some_number']
data = [['hello how are you doing','1.0'],
['hello', '1.0'],
['bye bye bye bye', '0.0'],
['no', '0.0'],
['yes', '1.0'],
['Who let the dogs out', '0.0'],
['1 + 1 + 1 + 2', '1.0']]
df = pd.DataFrame(columns=columns, data=data)
我想输出字数大于 3 的行。
在这里,行 'hello how are you doing', 'bye bye bye bye', 'Who let the dogs out', '1 + 1 + 1 + 2'我的方法不起作用: df[len(df.answer) > 3]输出: KeyError: True

最佳答案

如果分隔符是 ' ' ,你可以试试series.str.count ,否则您可以替换 sep

n=3
df[df['answer'].str.count(' ').gt(n-1)]
包含多个空格 #credits @piRSquared
df['answer'].str.count('\s+').gt(2)
或者使用列表理解:
n= 3
df[[len(i.split())>n for i in df['answer']]] #should be faster than above
                    answer some_number
0 hello how are you doing 1.0
2 bye bye bye bye 0.0
5 Who let the dogs out 0.0
6 1 + 1 + 1 + 2 1.0

关于python - 输出一列字数大于 3 的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66570375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com