2

Consider that I have this example dataframe:

d = {'Gender': [1,1,0,1,0], 'Employed': [1,0,0,1,1]}

I would like this to be an array of this form:

[[1 1 0 1 0][1 0 0 1 1]]

When I run

d[['Gender', 'Employed']].to_numpy()

I get an array of the form [[1 1][1 0][0 0][1 1][0 1]].

3 Answers 3

1

You can transpose the DataFrame, get the underlying numpy array with values method and convert to a list with tolist method:

out = pd.DataFrame(d).T.values.tolist()

Output:

[[1, 1, 0, 1, 0],
 [1, 0, 0, 1, 1]]
Sign up to request clarification or add additional context in comments.

Comments

1

Just transpose it.

d[['Gender', 'Employed']].values.T

Comments

0

This can get your expected output

import pandas as pd
df = pd.DataFrame({'Gender': [1,1,0,1,0], 'Employed': [1,0,0,1,1]})
print([df[column].to_list() for column in df])

Output:

[[1, 1, 0, 1, 0], [1, 0, 0, 1, 1]]

Let me know if you expect more from here.

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.