我想从CSV文件中获取数据并放入两个不同的数组变量中

时间:2016-04-10 18:05:13

标签: python csv python-3.x pandas

我在python中有这个代码。导入CSV文件。

Location = r'C:\assign\lrdataset.csv'
df = pd.read_csv(Location, names=['Xi','Yi'])
print(df.columns)

打印显示:

   Xi          Yi
0   -2.552990 -218.408328
1    1.764052  155.118872
2   -1.791328 -128.884326
3   -1.214077  -91.571734
4   -1.444940 -122.267726
5    0.195070   12.248124
6    1.480515  135.444007
........

但我想把这两个输入和输出值分成两个不同的变量,数组形式。像这样。

X=np.array([[-2.552990],[1.764052],[-1.791328],[-1.214077]])

Y=np.array([[-218.408328],[155.118872],[-128.884326],[-91.571734]])

2 个答案:

答案 0 :(得分:2)

就是你想要的:

In [43]: X = df.Xi.reshape((len(df), 1))

In [44]: X
Out[44]:
array([[-2.55299 ],
       [ 1.764052],
       [-1.791328],
       [-1.214077],
       [-1.44494 ],
       [ 0.19507 ],
       [ 1.480515]])

如果你想要围绕你的价值观:

In [62]: df.Xi.round(5).reshape(len(df), 1)
Out[62]:
array([[-2.55299],
       [ 1.76405],
       [-1.79133],
       [-1.21408],
       [-1.44494],
       [ 0.19507],
       [ 1.48052]])

答案 1 :(得分:2)

您可以使用reshape尝试shape

print df.shape
(7, 2)

X = df.Xi.reshape((df.shape[0],1))
print X
[[-2.55299 ]
 [ 1.764052]
 [-1.791328]
 [-1.214077]
 [-1.44494 ]
 [ 0.19507 ]
 [ 1.480515]]

Y = df.Yi.reshape((df.shape[0],1))
print Y
[[-218.408328]
 [ 155.118872]
 [-128.884326]
 [ -91.571734]
 [-122.267726]
 [  12.248124]
 [ 135.444007]]