# How to get a dataframe values in one single column for the following dataset?

m38 m78 alpha

4.4717 4.8745
4.4569 4.6491
4.5101 4.7262
4.4407 4.8234
4.1184 4.3862
3.8448 4.2816
3.7246 4.6183
3.2857 4.6744

For the above sample data (actually 8000 rows) i need to calculate alpha(column), using the log function.

In: alpha = (np.log([dataset.m38,dataset.m78])/np.math.log(38,78.7))

OUT: array([[1.79754463, 1.90105609], [1.7935659 , 1.8442364 ], [1.80780671, 1.86397626], ..., [2.06431358, 2.40416332], [2.08820691, 2.41635699], [2.09982107, 2.39551918]])

I converted this into dataframe by using the below code:

In: alpha = pd.DataFrame(data = alpha)

OUT: 2 rows × 8046 columns

Now, i used alpha.T to get this in 8046 rows and 2 columns.

Why i am getting 2 columns not one? and how should i convert it into one column?

• You're computing the log of each of the two columns, so you get two numbers out. If you want one column, you'll need to elaborate on what purpose the computation is serving. Mar 6, 2019 at 22:00
• Yes correct two columns are used here m38 & m78, but i created alpha variable to get the output of the formula (np.log([dataset.m38,dataset.m78])/np.math.log(38,78.7)). I should get one number as output against each row? and therefore one column. Mar 7, 2019 at 6:50

## 1 Answer

You are not getting 2 columns. You are getting 2 rows.

The function alpha = (np.log([dataset.m38,dataset.m78])/np.math.log(38,78.7)) returns a 2-dimensional array. Putting in:

         m38     m78
0  4.4717  4.8745
1  4.4569  4.6491
2  4.5101  4.7262
3  4.4407  4.8234
4  4.1184  4.3862


gives us: [[0.41174795 0.41083658 0.4140986 0.40983552 0.38912198] [0.43545842 0.42244323 0.42696487 0.43256132 0.40644075]]

This is due to the fact that np.log([dataset.m38,dataset.m78]) returns a 2-dimensional array.

Regarding the flipped columns/ row issue: Once your function doesn't return a 2-dimensional array, it will most likely return a Pandas Series which will have one column and 8000 rows.

If you want to turn that into a DataFrame, you can use alpha = pd.DataFrame({'alpha':alpha})

Think about what your formula does. Putting in [dataset.m38,dataset.m78] will do the calculation for both columns seperately resulting in 2 different arrays.

• alpha = ln(m38/m78.7)/ ln(38/78.7) I think i am doing something wrong in python code which is why it is giving 2-D array. I have to use the above formula in python so that it can calculate the values of all 8000 rows in one column as 1-D array 'alpha'. After that i have to add the alpha in my 'dataset' variable which is a dataframe. Can someone help me here? Mar 7, 2019 at 15:58
• Just change the function to alpha = (np.log(dataset.m38/dataset.m78)/np.log(38/78.7)) Mar 8, 2019 at 7:44