[英]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).我打算列出 50 个随机数,从 1 到 100,然后取数字的平方和平方根,然后我会将它们放入一个不错的 PD.Dataframe(第一个列表是随机数,它们是行和另外两个列表是列)。 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!
谢谢!
Your logic is a little off here.你的逻辑有点不对劲。 I think np.stack is a little more intuitive here as well.
我认为 np.stack 在这里也更直观一些。 You can also remove y from the stack if you would only like the random numbers one time.
如果您只想随机数一次,也可以从堆栈中删除 y。
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'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.