2

I have this code in python. Importing CSV file.

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

Print shows this:

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

But i want to take this two input and output values into two different variables, in array forms. Like this.

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

Y=np.array([[-218.408328],[155.118872],[-128.884326],[-91.571734]])
Jawwad Rafiq
  • 327
  • 3
  • 20
  • 1
    That's nice. What have you tried? And where are you getting the values for `X` and `Y`? – MattDMo Apr 10 '16 at 18:09
  • 1
    If you didnt understand what am i asking, why did you vote down it? Data is coming from CSV file. Simple that. CSV is excel file. That containing 2 columns. – Jawwad Rafiq Apr 10 '16 at 18:13
  • Concern is not those values of X and Y . Concern is only to representation of Data. – Jawwad Rafiq Apr 10 '16 at 18:15
  • Back to my original question - what have you tried so far? Stack Overflow is not a code-writing or tutorial service. Please [edit] your question and post what you have tried so far, including example input, expected output, and the **full text** of any errors or tracebacks. – MattDMo Apr 10 '16 at 18:16
  • What i have tried is front of you . I have tried this portion of code that is not giving output as i want. its giving representation in row shape. i want to put data into two variables. as i mentioned above in code. – Jawwad Rafiq Apr 10 '16 at 18:20
  • 1
    `X = df.Xi.values` – Alexander Apr 10 '16 at 18:24

2 Answers2

2

is that what you want:

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

if you want to round your values:

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]])
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • [ 3.55481793e-01] [ -5.35270165e-01] [ -3.04614305e+00] [ 1.99300197e-01] [ 5.19453960e-02] [ -8.59307670e-02] [ -7.07505698e-01] [ -5.54309627e-01] [ -7.92286662e-01] [ -5.90057646e-01] [ 6.89818165e-01] [ -1.54477110e+00] [ 1.03493150e-02] [ -1.53624369e+00] [ 8.64436199e-01] its giving output in shape of these incorrect values in column. – Jawwad Rafiq Apr 10 '16 at 18:43
  • @JawwadRafiq, try to set format for float values: `pd.options.display.float_format = '{:.5f}'.format` – MaxU - stand with Ukraine Apr 10 '16 at 18:50
  • i copied this line as it is in my code. but output is same . [ 3.55481793e-01].... . – Jawwad Rafiq Apr 10 '16 at 19:00
  • @JawwadRafiq, you may want to check [this](http://stackoverflow.com/questions/21008858/formatting-floats-in-a-numpy-array) – MaxU - stand with Ukraine Apr 10 '16 at 19:02
  • [ 3.55480000e-01] [ -5.35270000e-01] [ -3.04614000e+00] [ 1.99300000e-01] [ 5.19500000e-02] [ -8.59300000e-02] [ -7.07510000e-01] [ -5.54310000e-01] [ -7.92290000e-01] [ -5.90060000e-01] [ 6.89820000e-01] [ -1.54477000e+00] – Jawwad Rafiq Apr 10 '16 at 19:10
  • @JawwadRafiq, well this has nothing to do with your question. Try to use google asking how to present you numpy float values in desired format – MaxU - stand with Ukraine Apr 10 '16 at 19:14
  • i do not want to present in any format, i just want to show values as they were showing before . I wan to print them as it is. – Jawwad Rafiq Apr 10 '16 at 19:15
  • I am unable to present it in correct format. please help :( – Jawwad Rafiq Apr 10 '16 at 19:38
2

You can try reshape with 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]]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • [ 3.55480000e-01] [ -5.35270000e-01] [ -3.04614000e+00] [ 1.99300000e-01] [ 5.19500000e-02] [ -8.59300000e-02] [ -7.07510000e-01] [ -5.54310000e-01] [ -7.92290000e-01] [ -5.90060000e-01] [ 6.89820000e-01] [ -1.54477000e+00] – Jawwad Rafiq Apr 10 '16 at 19:40
  • I am unable to present it in correct format. please help :( – Jawwad Rafiq Apr 10 '16 at 19:40
  • How it works? Still problem? Maybe help [this](http://stackoverflow.com/questions/9777783/suppress-scientific-notation-in-numpy-when-creating-array-from-nested-list) - `np.set_printoptions(suppress=True)` – jezrael Apr 10 '16 at 20:54
  • [ -52.57817499] [-268.2346978 ] [ 2.81502832] [ 24.85009055] [ 18.77058224] [ -31.35049768] [ -29.87467044] [ -52.92511068] [ -41.7770076 ] – Jawwad Rafiq Apr 10 '16 at 21:28
  • I am very surprised that you unaccept my solution. I think my comment under this solution help you with formatting problem. Can you explain why you unaccept? Thank you. – jezrael Apr 15 '16 at 19:50
  • it is working fine , but output in different format. but its ok. – Jawwad Rafiq May 02 '16 at 04:23
  • 1
    Ok, no problem. Nice day. – jezrael May 02 '16 at 04:47