如何将复杂的数据帧值转换为 python 3 中的浮点数?

How to convert complex data frame values to floats in python 3?

我使用傅里叶变换将一个数据帧从时域转换为频域,得到了 imaginary/complex 值。我需要将 DataFrame 转换为浮点数以便能够对数据进行分类。 我怎样才能转换它?

df = pd.DataFrame([complex(x,y) for x,y in 
          zip(np.random.randn(3),np.random.randn(3))])
print(df)

Out:
                                  0
0  (0.815555184453+0.942659258939j)
1  (0.725136694628+0.999826686401j)
2  (0.311981899931+0.309615235755j)

如果你想要复数的大小,你可以取模(极坐标中的r):

df.applymap(np.absolute)

Out:
          0
0  1.246490
1  1.235102
2  0.439539

如果您想丢失可以转换为浮点数的虚部:

df.astype(np.float64)

Out:
          0
0  0.815555
1  0.725137
2  0.311982