python pandas complex number

后端 未结 4 1118
挽巷
挽巷 2020-12-12 02:54

I am using pandas which very efficiently sorts/filters the data they way I need.

This code worked fine, until I changed the last column to a complex number; now I ge

4条回答
  •  孤街浪徒
    2020-12-12 03:00

    I tried implementing the lambda but was getting a error:

    ValueError: complex() arg is a malformed string

    I found out I had to eliminate the spaces as well as change the 'i' character to 'j' Here's my code:

    for tits in df.columns:
        if df[tits].dtypes =='O':
            df[tits] = df[tits].str.replace('i','j')
            df[tits] = df[tits].str.replace(' ','')
            df[tits] = df[tits].apply(lambda x: np.complex(x))
    print(df[df.columns[1]])
    print(df.dtypes)
    

提交回复
热议问题