[英]Str.Contains show only True values
我在大型數據框上使用str.contains,我需要一種使str.contains返回我的str.contains函數為True的記錄的方法。 (數據幀長幾千行,我正在尋找8個真實的響應)。
謝謝!
aa = filtered_to_df.body.str.contains('AA')
aa.head(10)
Out[312]:
15864 False
18040 False
22576 False
28092 False
32800 False
33236 False
38027 False
41222 False
46647 False
87645 False
Name: body, dtype: bool
重要區別: str.contains
實際上並不過濾您的數據str.contains
或序列,它僅返回與應用序列相同維度的布爾向量。
例如:如果您有這樣的系列:
my_series = pd.Series(['hello world', 'hello', 'world'])
print(my_series)
0 hello world
1 hello
2 world
dtype: object
在其上使用str.contains("hello")
將返回一系列大小為3的str.contains("hello")
,因為它將為該系列中的每個單元格提供True / str.contains("hello")
該單元格包含單詞“ hello”嗎?
my_series = pd.Series(['hello world', 'hello', 'world'])
print(my_series.str.contains("hello"))
0 True
1 True
2 False
dtype: bool
要實際過濾數據框或序列,您需要使用切片操作對其進行環繞。
my_series[my_series.str.contains("hello")]
0 hello world
1 hello
dtype: object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.