如何在给定位置选择包含特定子字符串的行 - python

I.M*_*.M. 1 python substring python-3.x pandas

我正在使用一个看起来像这样的大数据框:

     id      time1      time2   data    
0   id1   06:24:00   06:24:00      A
1   id2   07:24:00   07:24:00      A
2   id3   08:24:00   08:24:00      B
Run Code Online (Sandbox Code Playgroud)

我想选择具有所有行time1和/或time223:xx:yy格式。

我尝试使用以下代码,但速度非常慢,因此我正在寻找更有效的方法:

list_ = list()

for idx in df.index:
    if ('23' in df.time1[:2]) | ('23' in df.time2[:2]):
        list_.append(df.loc[df.index == idx])  ###--- Here I wanted to get a list of indexes so I could do a simple df.loc[] afterward
Run Code Online (Sandbox Code Playgroud)

我还尝试了以下代码,但所有代码都引发了错误:

df.loc[df.time1[:2] == '23']
df.loc['23' in df.time1[:2]]
df[df.time1[:2].str.contains('23')]

> IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?任何帮助将不胜感激。

jez*_*ael 5

使用Series.str.startswithwith |for bitwiseOR&for bitwise AND

df[df.time1.str.startswith('23') | df.time2.str.startswith('23')]
Run Code Online (Sandbox Code Playgroud)

如果想比较字符串的前 2 个值添加str[:2]索引:

df[df.time1.str[:2].eq('23') | df.time2.str[:2].eq('23')]
Run Code Online (Sandbox Code Playgroud)