keep = [] #hold all the rows you want to keep
for key in frame_dict.keys():
frame = frame_dict[key]
keep.append(
frame[frame['A'].astype(str).str.contains('^\d\d02', regex=True)].copy()
) #append the rows matching regex for start of word (^), digit (\d), digit (\d), 02
final = pd.concat(keep) #concatenate the matching rows
试试这个:
第一行创建一个索引器,该索引器检查A列的第3个和第4个字符,并为任何带有“02”的内容返回布尔索引器True/false。在
第二行在应用索引器后从原始数据帧创建一个新的数据帧。在
编辑:上面的解决方案适用于数据帧字典。在
^{pr2}$像下面这样的东西怎么样,其中
d
是你的口述:使用由示例数据帧重复3次和键组成的dict
^{pr2}$0-2
这就产生了:
这应该比创建行列表或使用列表理解更节省内存,因为它使用生成器表达式。由于直接索引(假设数据值是标准化的),它也应该比使用regex更快。在
如果您不喜欢组合数组的索引,可以始终
reset_index()
。例如:相关问题 更多 >
编程相关推荐