0

I'm trying to transform a numpy nd array into a pandas column, but the data is coming with brackets.

This is my np array:

array([[[  7.10105920e+07],
        [  9.18736320e+07],
        [  8.35562800e+07],
        [  7.16590640e+07],
        [  8.28060960e+07],
        [  6.77042000e+07],
        [  7.07195360e+07],
        [  1.04754616e+08],
        [  7.27420400e+07],
        [  7.33461760e+07],
        [  6.34156040e+07],
        [  8.00440800e+07],

This is how I'm sending to the dataframe:

predictions = pd.DataFrame()
predictions['y_test'] = Y_test[0].tolist()

This is what I'm getting:

           y_test
0    [71010592.0]
1    [91873632.0]
2    [83556280.0]
3    [71659064.0]
4    [82806096.0]
5    [67704200.0]
6    [70719536.0]
7   [104754616.0]
8    [72742040.0]
9    [73346176.0]

How can I remove the brackets ([])?

0

4 Answers 4

4

It looks like a 3D array. You can pass its first element to the DataFrame constructor:

pd.DataFrame(Y_test[0], columns=['y_test'])
Out: 
         y_test
0    71010592.0
1    91873632.0
2    83556280.0
3    71659064.0
4    82806096.0
5    67704200.0
6    70719536.0
7   104754616.0
8    72742040.0
9    73346176.0
10   63415604.0
11   80044080.0

A better alternative from Divakar is to use squeeze:

pd.DataFrame(arr.squeeze(), columns=['y_test'])
Out: 
         y_test
0    71010592.0
1    91873632.0
2    83556280.0
3    71659064.0
4    82806096.0
5    67704200.0
6    70719536.0
7   104754616.0
8    72742040.0
9    73346176.0
10   63415604.0
11   80044080.0
Sign up to request clarification or add additional context in comments.

2 Comments

Rather than being bothered to find out the non-singleton dim, I would just use squeeze.
@Divakar yeah much better. Thanks.
2

It seems you have a 3d array, you could try:

predictions['y_test'] = Y_test[0,:,0]

predictions

#       y_test
#0  71010592.0
#1  91873632.0
#2  83556280.0
#3  71659064.0
#4  82806096.0
#5  67704200.0
#6  70719536.0
#7  104754616.0
#8  72742040.0
#9  73346176.0
#10 63415604.0
#11 80044080.0

Comments

1

Couple of ways you could do

Option 1. Numpy indexing

predictions['y_test'] = Y_test[0,:,0]

Option 2. Flatten using list comprehension

predictions['y_test'] = [x[0] for x in Y_test[0]]

Option 3. Numpy flatten function

predictions['y_test'] = Y_test.flatten()

Comments

0
prediction = pd.DataFrame(Y_test.flatten(), columns=['y_test'])
prediction.head()

    y_test
    0     1.0
    1     1.0
    2     1.0
    3     1.0
    4     1.0

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.