yasuomain
yasuomain

Reputation: 55

Converting pandas dataframe to an array

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.

Upvotes: 0

Views: 60

Answers (2)

cs95
cs95

Reputation: 402323

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

Upvotes: 1

BENY
BENY

Reputation: 323226

T + as_matrix

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

Upvotes: 1

Related Questions