在python/pandas中清理multitype数据框的值,我想修剪字符串.我目前正在两个指令中执行此操作:
import pandas as pd
df = pd.DataFrame([[' a ', 10], [' c ', 5]])
df.replace('^\s+', '', regex=True, inplace=True) #front
df.replace('\s+$', '', regex=True, inplace=True) #end
df.values
Run Code Online (Sandbox Code Playgroud)
这很慢,我能改进什么?
使用 Python 和 OpenCV,我正在检测二进制掩模的轮廓:
import numpy as np
import cv2
import matplotlib.pyplot as plt
mask = np.zeros(20000, dtype=np.uint8).reshape(100, 200)
mask[5:-5,5:-5] = 255
mask[10:70,40:80] = 0
plt.subplot(121)
plt.imshow(mask, cmap='Greys_r', interpolation='none')
_, contours, hierarchy = cv2.findContours(mask.copy(),
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE,
offset=(0, 0))
Run Code Online (Sandbox Code Playgroud)
产生预期的行为:
plt.subplot(122)
cv2.drawContours(mask, contours, -1, (127, 127, 127), 2)
plt.imshow(mask, cmap='Greys_r', interpolation='none')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法理解完全激活面罩的结果:
mask = np.ones(20000, dtype=np.uint8).reshape(100, 200)
mask *=255
_, contours, hierarchy = cv2.findContours(mask.copy(),
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE,
offset=(0, 0))
print contours[0]
Run Code Online (Sandbox Code Playgroud)
其产生:
(1 1), (1 98), (198 98), (198 1)
Run Code Online (Sandbox Code Playgroud)
代替 …