在计算期间将np.array重新排列为float64数据时出错

时间:2017-12-15 11:23:51

标签: python python-3.x python-2.7 python-requests

我正在从CSV文件中读取变量“RF”,然后我有一个“数组”数据。我想在两个变量之间加上一些方程式。因此,我正在将我的数组数据转换为“RF”数据格式。在我的CSV文件中,我有8个变量,但目前我只读取一个变量。当我执行代码时,它会给出一些错误。

import numpy as np 
import pandas as pd 
import csv
data_df = pd.read_csv('poly2_decpred_juneRF.csv', header=0, nrows=67)
y1 = data_df['RF']
y1.head(67)

finalpred1=[2.22, 2.33, 3.33, 1.21, 2.22, 2.33, 3.33, 1.21, 2.22, 2.33, 3.33, 1.21, 2.22, 2.33, 3.33, 1.21, 2.33]
s = pd.Series(finalpred1, name='RF')
print(s)
y111=y1[50:67]
print(y111)
diff = ((y111-s)/y111)*100
print(diff)

输出如下:

0    NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
6    NaN
7    NaN
8    NaN
9    NaN
10   NaN
11   NaN
12   NaN
13   NaN
14   NaN
15   NaN
16   NaN
50   NaN
51   NaN
52   NaN
53   NaN
54   NaN
55   NaN
56   NaN
57   NaN
58   NaN
59   NaN
60   NaN
61   NaN
62   NaN
63   NaN
64   NaN
65   NaN
66   NaN
Name: RF, dtype: float64

CSV数据集以以下格式提供:

RF,Nino,IOD,SOI,NAO,TNA,TSA,MEI
103.77,-1.04,0.20,2.70,-1.02,0.18,-0.92,-1.26
83.99,0.71,0.16,-1.70,1.32,-0.07,0.00,0.48
86.97,-0.55,0.21,-1.30,-0.43,0.55,-0.14,-0.12
152.58,0.15,0.11,-1.00,-0.47,0.17,-0.41,0.32
230.83,-0.79,0.18,0.60,0.69,-0.30,-0.23,-1.11
223.45,-1.52,-0.06,-0.50,0.17,0.30,-0.57,-1.88
118.26,-0.58,0.08,1.50,0.10,0.05,-0.49,-1.02
93.58,1.18,-0.02,-0.60,0.12,0.17,-0.16,1.25
..........................................
132.30,2.56,0.49,0.10,2.24,0.42,0.70,2.11
110.00,-0.51,-0.07,0.40,0.48,0.52,0.67,-0.12

总行数为67: 请帮帮我

1 个答案:

答案 0 :(得分:1)

我认为您可以使用Series

s = pd.Series(finalpred1, name='RF')

numpy array

s = np.array(finalpred1)

因为有不同的索引值且不匹配,所以输出为NaN s

但更好的是仅过滤RF列并跳过第一个50行:

s = pd.read_csv('poly2_decpred_juneRF.csv', 
                header=0, 
                nrows=17, 
                skiprows=range(1, 50), 
                usecols=['RF'])