【发布时间】:2021-01-29 19:01:45
【问题描述】:
我正在使用如下所示的大数据框:
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
我想以23:xx:yy 格式选择所有具有time1 和/或time2 的行。
我尝试使用以下代码,但速度极慢,因此我正在寻找更高效的方法:
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
我也尝试了以下代码,但都引发了错误:
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
有没有办法做到这一点?任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x pandas substring