python pandas复数 [英] python pandas complex number

查看:249
本文介绍了python pandas复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用熊猫,它们可以非常有效地对他们所需的数据进行排序/过滤.

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 get an error.

返回self._cython_agg_general('mean')引发DataError('无数字 要聚合的类型")pandas.core.groupby.DataError:没有数字类型 汇总

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

该错误涉及我的第八列(带有复数),因为我想要平均值 我找不到将对象转换为复数的方法(据我了解,熊猫现在支持复数).

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).

这是我使用的代码.

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.

当我读取文件时,这是df输出.

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

<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)

这是输入文件的一小部分.

推荐答案

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

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)

这篇关于python pandas复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆