我不明白pandas
DataFrame
filter
。
import pandas as pd
df = pd.DataFrame(
[
['Hello', 'World'],
['Just', 'Wanted'],
['To', 'Say'],
['I\'m', 'Tired']
]
)
df.filter([0], regex=r'(Hel|Just)', axis=0)
我希望[0]
指定第一列作为要查看的列,axis=0
指定筛选行。我得到的是:
0 1
0 Hello World
我在期待
0 1
0 Hello World
1 Just Wanted
这应该有效:
df[df[0].str.contains('(Hel|Just)', regex=True)]
每the docs
看来,第一个可选参数
items=[0]
胜过第三个可选参数regex=r'(Hel|Just)'
。相当于
它只是选择索引值在
[0]
中沿0轴的行。为了得到想要的结果,可以使用
str.contains
创建一个布尔掩码, 并使用df.loc
选择行:相关问题 更多 >
编程相关推荐