Search code examples
pandasnumpydataframeserieslmplot

How to convert numpy one dimensional array to Pandas Series or Dataframe


I have spent quiet some time on what seems to be very easy thing. All I want is to convert a numpy array to a Series and then combine Series to make a dataframe. I have two numpy arrays.

import numpy as np

rooms = 2*np.random.rand(100, 1) + 3
price = 265 + 6*rooms + abs(np.random.randn(100, 1))

I wanted to convert rooms and price to series and then combine the two series into a dataframe to make lmplot

So could any one tell me how to do that? Thanks.


Solution

  • you can use ravel() to convert the arrays to 1-d data:

    pd.DataFrame({
         'rooms': rooms.ravel(),
        'price': price.ravel()
    })