Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe to 1-d array

I have a dataframe with lots of columns.

I first choose only one column from the dataframe by r_i = df.iloc[:, i: i + 1]

Then I want to turn this r_i into array simply by np.array(r_i).

the result I want is like: array([-1, -2, -3]). In other words, it should be array of one list.

However, it gives me array of one list which consists of sublists: array([[-1], [-2], [-3]]).

How do I prevent this from happening?

Thank you.

like image 985
Jun Seong Jang Avatar asked May 29 '26 05:05

Jun Seong Jang


1 Answers

df.values.flatten()

Here, df is your DataFrame.

like image 105
Santosh Katuwal Avatar answered May 31 '26 18:05

Santosh Katuwal