我正在处理一些从EXCEL导入并转换为列表的数据集。
import pandas as pd
import numpy as np
datfrms = []
for i in xls.sheet_names:
df = pd.read_excel(xls, i)
datfrms.append(df)
data_a = []
data_b = []
data_c = []
for dfs in datfrms:
data_a.append(dfs.loc[:,'data_a'])
data_b.append(dfs.loc[:,'data_b'])
data_c.append(dfs.loc[:,'data_c'])
然后,我想对数据进行一些计算,所以我决定在进行一些计算时将列表转换成numpy数组。
a = np.asarray([2 * (a + b) for a, b in zip(data_a, data_b])
b = np.asarray([c / 1000 for c in data_c])
因此,a
、b
和c
现在被定义为<class 'numpy.ndarray'>
,其形状为(13,)
,对应于我上面导入的13张表。每当我想访问第一个工作表的数据时,我就写,比如说,data_a[0]
。
然而,如果我想执行类似于AttributeError: 'Series' object has no attribute 'sqrt'
的操作,就会出现一个错误,说明AttributeError: 'Series' object has no attribute 'sqrt'
。
d = np.sqrt(a / b)
如果我手动去写,没有错误产生。
d0 = np.sqrt(a[0] / b[0])
d12 = np.sqrt(a[12] / b[12])
But if I use the type
function, d0
... d12
are now <class 'pandas.core.series.Series'>
, whereas a[0]
和 b[0]
are both <class 'numpy.ndarray'>
.
What am I doing wrong?
Why am I not allowed to perform a simple square-root operation?
我希望能添加数据,但我无法通过在Python中制作合成数据来重现数据格式,我怀疑这可能是问题的核心(即我在数据格式方面做错了什么)。
user32185分别要求输出a[0]
和b[0]
。
0 0.883871
1 0.885714
2 0.879378
3 0.865668
4 0.866014
5 0.860657
6 0.866071
7 0.884389
8 0.892339
9 0.892512
10 0.841590
11 0.841014
12 0.882200
13 0.857546
14 0.850576
15 0.853975
16 0.838710
dtype: float64
0 3.701151
1 3.701938
2 3.700758
3 3.690926
4 3.685027
5 3.688959
6 3.712556
7 3.786099
8 3.888745
9 3.956389
10 3.799078
11 3.799078
12 3.778627
13 3.669295
14 3.638620
15 3.606371
16 3.547379
Name: b, dtype: float64