pandas python熊猫复数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18919699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:10:25  来源:igfitidea点击:

python pandas complex number

pythonpandas

提问by Dimitris

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

我正在使用 Pandas,它可以非常有效地按照我需要的方式对数据进行排序/过滤。

This code worked fine, until I changed the last column to a complex number; now I get an error.

这段代码运行良好,直到我将最后一列更改为复数;现在我收到一个错误。

return self._cython_agg_general('mean') raise DataError('No numeric types to aggregate') pandas.core.groupby.DataError: No numeric types to aggregate

return self._cython_agg_general('mean') raise DataError('No numeric types to aggregate') pandas.core.groupby.DataError:没有要聚合的数字类型

The error refers to my eighth column (with the complex numbers) since I want the mean value I cannot find a way to convert the object to a complex number (from what I understand pandas now support complex numbers).

该错误是指我的第八列(带有复数),因为我想要平均值我找不到将对象转换为复数的方法(据我所知,Pandas现在支持复数)。

This is the code I use.

这是我使用的代码。

import numpy as np
import pandas as pd
df = pd.read_csv('final.dat', sep=",", header=None)
df.columns=['X.1', 'X.2', 'X.3', 'X.4','X.5', 'X.6', 'X.7', 'X.8']
df1 = df.groupby(["X.1","X.2","X.5"])["X.8"].mean().reset_index()

After that I get the error described above.

之后,我收到上述错误。

When I read my file, this is the df output.

当我阅读我的文件时,这是 df 输出。

<class 'pandas.core.frame.DataFrame'>
Int64Index: 21266 entries, 0 to 21265
Data columns (total 8 columns):
X.1    21266  non-null values
X.2    21266  non-null values
X.3    21266  non-null values
X.4    21266  non-null values
X.5    21266  non-null values
X.6    21266  non-null values
X.7    21266  non-null values
X.8    21266  non-null values
dtypes: float64(4), int64(3), object(1)

This is a small sample of the input file.

这是输入文件的一个小样本。

回答by Jeff

The parse doesn't support reading of complex directly, so do the following transform.

解析不支持直接读取复数,因此请执行以下转换。

In [37]: df['X.8'] = df['X.8'].str.replace('i','j').apply(lambda x: np.complex(x))

In [38]: df
Out[38]: 
          X.1         X.2  X.3   X.4    X.5  X.6  X.7                X.8
0   564991.15  7371277.89    0     1   1530  0.1    2   (92.289+151.96j)
1   564991.15  7371277.89    0     1   8250  0.1    2   (104.22-43.299j)
2   564991.15  7371277.89    0     1  20370  0.1    2    (78.76-113.52j)
3   564991.15  7371277.89    0     1  33030  0.1    2    (27.141-154.1j)
4   564991.15  7371277.89    0     1  47970  0.1    2     (-30.012-175j)
5   564991.15  7371277.89    0     1  63090  0.1    2  (-118.52-342.43j)
6   564991.15  7371277.89    0     1  93090  0.1    2  (-321.02-1541.5j)
7   564991.15  7371277.89    0     2   1530  0.1    2   (118.73+154.05j)
8   564991.15  7371277.89    0     2   8250  0.1    2   (122.13-45.571j)
9   564991.15  7371277.89    0     2  20370  0.1    2   (93.014-116.03j)
10  564991.15  7371277.89    0     2  33030  0.1    2    (38.56-155.08j)
11  564991.15  7371277.89    0     2  47970  0.1    2  (-20.653-173.83j)
12  564991.15  7371277.89    0     2  63090  0.1    2  (-118.41-340.58j)
13  564991.15  7371277.89    0     2  93090  0.1    2    (-378.71-1554j)
14  564990.35  7371279.17    0  1785   1530  0.1    2   (-15.441+118.3j)
15  564990.35  7371279.17    0  1785   8250  0.1    2  (-7.1735-76.487j)
16  564990.35  7371279.17    0  1785  20370  0.1    2  (-33.847-145.99j)
17  564990.35  7371279.17    0  1785  33030  0.1    2  (-86.035-185.46j)
18  564990.35  7371279.17    0  1785  47970  0.1    2  (-143.37-205.23j)
19  564990.35  7371279.17    0  1785  63090  0.1    2  (-234.67-370.43j)
20  564990.35  7371279.17    0  1785  93090  0.1    2  (-458.69-1561.4j)
21  564990.36  7371279.17    0  1786   1530  0.1    2    (36.129+128.4j)
22  564990.36  7371279.17    0  1786   8250  0.1    2   (39.406-69.607j)
23  564990.36  7371279.17    0  1786  20370  0.1    2   (10.495-139.48j)
24  564990.36  7371279.17    0  1786  33030  0.1    2  (-43.535-178.19j)
25  564990.36  7371279.17    0  1786  47970  0.1    2  (-102.28-196.76j)
26  564990.36  7371279.17    0  1786  63090  0.1    2   (-199.32-362.1j)
27  564990.36  7371279.17    0  1786  93090  0.1    2  (-458.09-1565.6j)

In [39]: df.dtypes
Out[39]: 
X.1       float64
X.2       float64
X.3       float64
X.4         int64
X.5         int64
X.6       float64
X.7         int64
X.8    complex128
dtype: object

In [40]: df1 = df.groupby(["X.1","X.2","X.5"])["X.8"].mean().reset_index()

In [41]:  df.groupby(["X.1","X.2","X.5"])["X.8"].mean().reset_index()
Out[41]: 
          X.1         X.2    X.5                  X.8
0   564990.35  7371279.17   1530     (-15.441+118.3j)
1   564990.35  7371279.17   8250    (-7.1735-76.487j)
2   564990.35  7371279.17  20370    (-33.847-145.99j)
3   564990.35  7371279.17  33030    (-86.035-185.46j)
4   564990.35  7371279.17  47970    (-143.37-205.23j)
5   564990.35  7371279.17  63090    (-234.67-370.43j)
6   564990.35  7371279.17  93090    (-458.69-1561.4j)
7   564990.36  7371279.17   1530      (36.129+128.4j)
8   564990.36  7371279.17   8250     (39.406-69.607j)
9   564990.36  7371279.17  20370     (10.495-139.48j)
10  564990.36  7371279.17  33030    (-43.535-178.19j)
11  564990.36  7371279.17  47970    (-102.28-196.76j)
12  564990.36  7371279.17  63090     (-199.32-362.1j)
13  564990.36  7371279.17  93090    (-458.09-1565.6j)
14  564991.15  7371277.89   1530  (105.5095+153.005j)
15  564991.15  7371277.89   8250    (113.175-44.435j)
16  564991.15  7371277.89  20370    (85.887-114.775j)
17  564991.15  7371277.89  33030    (32.8505-154.59j)
18  564991.15  7371277.89  47970  (-25.3325-174.415j)
19  564991.15  7371277.89  63090  (-118.465-341.505j)
20  564991.15  7371277.89  93090  (-349.865-1547.75j)

回答by Viktor Kerkez

Or you can parse it directly as a complex number by passing in a converter for that column when you read in the data:

或者,您可以通过在读取数据时传入该列的转换器来直接将其解析为复数:

pd.read_csv('final.dat', header=None,
            names=['X.1', 'X.2', 'X.3', 'X.4','X.5', 'X.6', 'X.7', 'X.8'],
            converters={'X.8': lambda s: np.complex(s.replace('i', 'j'))})

回答by athlonshi

For converting all columns, try pd.applymap(lambda s: np.complex(s.replace('i', 'j')))

要转换所有列,请尝试 pd.applymap(lambda s: np.complex(s.replace('i', 'j')))

回答by Brad123

I tried implementing the lambda but was getting a error:

我尝试实现 lambda,但出现错误:

ValueError: complex() arg is a malformed string

ValueError: complex() arg 是一个格式错误的字符串

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

我发现我必须消除空格并将“i”字符更改为“j”这是我的代码:

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)