我有以下代码用于绘制散点图:
plt.scatter(step1.values[step1['y_ocsvm'] == 1, 2], step1.values[step1['y_ocsvm'] == 1, 1], c = 'green', label = 'Normal')
plt.scatter(step1.values[step1['y_ocsvm'] == -1, 2], step1.values[step1['y_ocsvm'] == -1, 1], c = 'red', label = 'Outlier')
plt.scatter(step2.values[step2['y_ocsvm'] == 1, 2], step2.values[step2['y_ocsvm'] == 1, 1], c = 'green')
plt.scatter(step2.values[step2['y_ocsvm'] == -1, 2], step2.values[step2['y_ocsvm'] == -1, 1], c = 'red')
step1
和step2
是两个不同的数据帧。
假设我们有许多这样的不同数据帧,我想知道哪种更紧凑的方式编写上述代码?
我尝试过的事情:
我尝试创建可用数据帧名称的列表,并尝试遍历它们,但是会引发错误:
alldfs = [var for var in dir() if isinstance(eval(var), pd.core.frame.DataFrame)]
matches = [i for i in alldfs if 'step' in i]
for i,df in enumerate(matches):
print(matches[i])
plt.scatter(matches[i].values[matches[i]['y_ocsvm'] == 1, 2], matches[i].values[matches[i]['y_ocsvm'] == 1, 1], c = 'green', label = 'Normal')
plt.scatter(matches[i].values[matches[i]['y_ocsvm'] == -1, 2], matches[i].values[matches[i]['y_ocsvm'] == -1, 1], c = 'red', label = 'Outlier')
它引发的错误:
alldfs = [var for var in dir() if isinstance(eval(var), pd.core.frame.DataFrame)]
matches = [i for i in alldfs if 'step' in i]
for i,df in enumerate(matches):
print(matches[i])
plt.scatter(matches[i].values[matches[i]['y_ocsvm'] == 1, 2], matches[i].values[matches[i]['y_ocsvm'] == 1, 1], c = 'green', label = 'Normal')
plt.scatter(matches[i].values[matches[i]['y_ocsvm'] == -1, 2], matches[i].values[matches[i]['y_ocsvm'] == -1, 1], c = 'red', label = 'Outlier')
step1
Traceback (most recent call last):
File "<ipython-input-51-e73fcf40a967>", line 3, in <module>
plt.scatter(matches[i].values[matches[i]['y_ocsvm'] == 1, 2], matches[i].values[matches[i]['y_ocsvm'] == 1, 1], c = 'green', label = 'Normal')
AttributeError: 'str' object has no attribute 'values'
编辑1:
step1
数据框
ContextID BacksGas_Flow_sccm StepID y_ocsvm
7289973 0.5 1 1
7289973 0.333333333 1 1
7289973 0.416666667 1 1
7289973 0.416666667 1 1
7289999 0.333333333 1 1
7289999 0.333333333 1 1
7289999 0.25 1 1
7289999 0.416666667 1 1
7290650 0.333333333 1 1
7290650 0.166666667 1 -1
7290690 0.666666667 1 -1
7290690 0.583333333 1 1
step2
数据框
ContextID BacksGas_Flow_sccm StepID y_ocsvm
7289973 0.777777778 2 -1
7289973 0.777777778 2 -1
7289973 0.666666667 2 1
7289973 0.444444444 2 1
7289999 0.444444444 2 1
7289999 0.555555556 2 1
7290025 0.777777778 2 -1
7290025 0.666666667 2 1
7290650 0.444444444 2 1
7290650 0.888888889 2 -1
7290650 0.777777778 2 -1
7290650 0.555555556 2 1