我有一个包含 2 条线的图(由特定方程创建),我正在尝试根据我的数据相对于线的位置创建新的 Pandas 数据框。
In [1]: import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [2]: df = pd.DataFrame([ [-1.0,0],[-1.0,1.5],[-0.15,[0.5,0.5] ],columns=['x','y'])
df
Out[2]:
x y
0 -1.00 0.0
1 -1.00 1.5
2 -0.15 0.0
3 0.5 0.5
我有两条线将与 df 中的数据一起绘制,这些线定义为:
In [3]: def line1():
x1 = np.linspace(-2,500)
y1 = 0.61/(x1 - 0.05) +1.3
return x1,y1
def line2():
x2 = np.linspace(-2,0.3,500)
y2 = 0.61/(x2 - 0.47) + 1.19
return x2,y2
因此,从 df 开始,我想根据点所在的 4 个位置(例如 df_red、df_green、df_black、df_blue)创建 4 个新数据帧。 我目前的想法是尝试这样的事情:
In [4]: line_orange = line1()
line_magenta = line2()
df_red = df[ (df.x < line_orange) & (df.y < line_magenta) ]
df_green = df[ (df.x < -0.9) & (df.y > line_magenta) ]
df_black = df[ (df.x < line_magenta) & (df.x > line_orange) ]
df_blue = df[ (df.x > line_magenta) ]
现在我不能 100% 确定这些参数将在哪里正确分类我的点,但我无法检查,因为这种分类不起作用 - 它引发:
ValueError: operands could not be broadcast together with shapes (4,) (2,500)
我愿意接受对我的观点进行分类的方法的建议。我不想遍历我的实际数据帧,因为它非常大。不要担心我的实际分类,只要我找到一种拆分数据的方法,我就可以使用它,谢谢!