Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a 2D numpy array to dataframe rows

I have a list of list that I would like to make it as a row. The closest I got was using this post. However, I could not get my answer.

For example lets say I have a testarray of values,

([[ 26.85406494], 
   [ 27.85406494],
   [ 28.85406494],
   [ 29.85406494],
   [ 30.85406494],
   [ 31.85406494],
   [ 32.85406494],
   [ 33.85406494],
   [ 34.85406494],
   [ 35.85406494],
   [ 36.85406494],
   [ 37.85406494],
   [ 38.85406494],
   [ 39.85406494],
   [ 40.85406494],
   [ 41.85406494]])

I need like as a pandas DataFrame row like this,

  Row_value
0 26.85406494
1 27.85406494
2 29.85406494
...

I tried the following,

df = pd.DataFrame({'Row_value':testarray})

I get an error,

ValueError: If using all scalar values, you must pass an index

How can I pass those values with an index?

like image 791
i.n.n.m Avatar asked Aug 30 '17 14:08

i.n.n.m


People also ask

How can you convert a NumPy array into a Pandas DataFrame Mcq?

To convert a numpy array to pandas dataframe, we use pandas. DataFrame() function of Python Pandas library.

Is NumPy array faster than Pandas DataFrame?

NumPy performs better than Pandas for 50K rows or less. But, Pandas' performance is better than NumPy's for 500K rows or more. Thus, performance varies between 50K and 500K rows depending on the type of operation.

How do you make a 2D NumPy array from a DataFrame?

It is quite easy to transform a pandas dataframe into a numpy array. Simply using the to_numpy() function provided by Pandas will do the trick. This will return us a numpy 2D array of the same size as our dataframe (df), but with the column names discarded.


2 Answers

Use DataFrame constructor only with parameter columns:

df = pd.DataFrame(a, columns=['a'])
print (df)
            a
0   26.854065
1   27.854065
2   28.854065
3   29.854065
4   30.854065
5   31.854065
6   32.854065
7   33.854065
8   34.854065
9   35.854065
10  36.854065
11  37.854065
12  38.854065
13  39.854065
14  40.854065
15  41.854065
like image 133
jezrael Avatar answered Oct 03 '22 20:10

jezrael


Try a .reshape(-1, ):

df = pd.DataFrame({'Row_value':testarray.reshape(-1, )})
df

    Row_value
0   26.854065
1   27.854065
2   28.854065
3   29.854065
4   30.854065
5   31.854065
6   32.854065
7   33.854065
8   34.854065
9   35.854065
10  36.854065
11  37.854065
12  38.854065
13  39.854065
14  40.854065
15  41.854065

It looks like your testarray is a 2D array with 1 column. Convert it to a 1D array.


The alternative is to not pass a dictionary, but instead pass testarray as is, like in jezrael's answer.

like image 29
cs95 Avatar answered Oct 03 '22 20:10

cs95