The shape
The shape

Reputation: 359

ValueError: Must pass 2-d input. shape=(1, 50, 2)

I was going to make a list of 50 random numbers, one to one hundred, and take the square and square root for the numbers, and I would put them into a nice PD.Dataframe (the first list is the random numbers which are the rows and the two other lists are the columns). My Code looks like this:

import random
import math
import numpy as np
import pandas as pd
y = random.sample(range(1, 100), 50)
y1 = [f**2 for f in y]
y2 = [round(math.sqrt(f2)) for f2 in y]
whole = y + y1 + y2
whole2 = (np.array(whole)).reshape((50,3))
df = pd.DataFrame([whole2[:,1:]], index=whole2[:,0], columns=['Number', 'Square','Square Root'])

I would appreciate if some one could tell me where and how I went wrong. Thanks!

Upvotes: 2

Views: 5736

Answers (1)

Michael Lee
Michael Lee

Reputation: 630

Your logic is a little off here. I think np.stack is a little more intuitive here as well. You can also remove y from the stack if you would only like the random numbers one time.

import random
import math
import numpy as np
import pandas as pd
y = random.sample(range(1, 100), 50)
y1 = [f**2 for f in y]
y2 = [round(math.sqrt(f2)) for f2 in y]
whole = np.stack([y1, y2], axis=-1)
df = pd.DataFrame(whole, index=y, columns=['Square','Square Root'])

enter image description here

Upvotes: 2

Related Questions