0

I have a csv file named "transactions4.csv" that contains values that look like:

column 1|column 2

--------|---------
12345   | 10
23456   | -15
12376   | 10
56842   | 25
45678   | -5 
78324   | 20

Here's what I have so far:

import pandas as pd
transactionsFileName = "transactions4.csv"
df = pd.read_csv(transactionsFileName)
print(df.to_string())

This prints the values from the file, but I'm not sure how to put each column into an array.

1
  • But why do you want an array? usually a Series is sufficient i.e. df["column 1"]. Commented Oct 21, 2017 at 1:48

2 Answers 2

1

You could also query the .values attribute.

x = df.values.T

print(x)
array([[12345, 23456, 12376, 56842, 45678, 78324],
       [   10,   -15,    10,    25,    -5,    20]])

If you want each column in a separate array, just unpack them:

i, j = x

print(i)
array([12345, 23456, 12376, 56842, 45678, 78324])

print(j)
array([ 10, -15,  10,  25,  -5,  20])
Sign up to request clarification or add additional context in comments.

Comments

1

T + as_matrix

df.T.as_matrix()
Out[70]: 
array([[12345, 23456, 12376, 56842, 45678, 78324],
       [   10,   -15,    10,    25,    -5,    20]], dtype=int64)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.