numpy read .csv with complex number

后端 未结 4 1071
-上瘾入骨i
-上瘾入骨i 2021-01-07 03:43

stackoverflow,

I have a matrix containing complex numbers (ex. -2.2982235934153075E-11+2.1179547211742553E-9i) that I need to import to a numpy array. I\'ve been us

4条回答
  •  隐瞒了意图╮
    2021-01-07 04:14

    The following might be an option to obtain a NumPy array from multi-column complex-numbered .csv file:

    Say we have a file.csv containing two rows and three columns of complex numbers:

    -0.00034467+0.j,         0.00493246+0.j,         0.00365753-0.00361799j
    -0.00782533-0.00081274j,-0.00402968+0.01065282j,-0.01345174+0.00464461j
    

    The following will yield a NumPy array:

    filename = 'file.csv'
    data = pd.read_csv(filename, sep=",", header=None)
    data = data.applymap(lambda s: np.complex(s.replace('i', 'j'))).values
    

    Checking if data is a NumPy array:

    >> type(data)
    numpy.ndarray
    

    PS: The answer is based on this answer.

提交回复
热议问题