从DataFrame创建面板(Create Panel from DataFrame)
我是python和pandas的新手。 我在Pandas中创建Panel时遇到问题。
def sq_error(w0,w1,x,y):
return (y – (w0 – w1 * x)) ** 2;
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],
row[‘Height’]), axis=1)
df = data[[‘height_error’,’Weight’]]
d[‘df’] = df
d[‘w1’] = w1
p = pd.Panel(d)
我得到错误’int’对象没有属性’形状’
我试过列表
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],
row[‘Height’]), axis=1)
l = df[[‘height_error’,’Weight’]].values.tolist()
d[‘df’] = l
d[‘w1’] = w1
p = pd.Panel(d)
但仍然有同样的错误
I am a new to python and pandas. And I have a problem in creating Panel at pandas.
def sq_error(w0,w1,x,y):
return (y – (w0 – w1 * x)) ** 2;
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],
row[‘Height’]), axis=1)
df = data[[‘height_error’,’Weight’]]
d[‘df’] = df
d[‘w1’] = w1
p = pd.Panel(d)
I’m getting error ‘int’ object has no attribute ‘shape’
I tried with list
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],
row[‘Height’]), axis=1)
l = df[[‘height_error’,’Weight’]].values.tolist()
d[‘df’] = l
d[‘w1’] = w1
p = pd.Panel(d)
But still getting same error
原文:https://stackoverflow.com/questions/40151459
2020-08-24 18:08
满意答案
熊猫正在尝试访问w1作为数据框,但它是一个整数。 所以当然它没有形状属性
你应该写d [w1] = df
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],row[‘Height’]), axis=1)
l = df[[‘height_error’,’Weight’]].values.tolist()
d[w1] = df
p = pd.Panel(d)
因此Pandas将它作为一个整数键和DataFrame值的字典来接受。
Pandas is trying to access w1 as a dataFrame but it’s an int. So of course it does not have a shape attribute
You should write d[w1]=df
d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],row[‘Height’]), axis=1)
l = df[[‘height_error’,’Weight’]].values.tolist()
d[w1] = df
p = pd.Panel(d)
Thus Pandas will accept it as a dictionary with integer keys and DataFrame values.
2016-10-20
相关问答
使用两个for循环来解决此问题: import pandas as pd
rows=range(0,3)
pl=pd.Panel()
first for loop to create dataframe
for i in range(0,3):
pl[i]=pd.DataFrame()
Second for loop to assign values
for i in range(0,3):
for j in rows:
pl[i].set_va…
选项1 pd.concat pd.concat({i: d.set_index(‘context’).time for i, d in pn.iteritems()}).unstack()
context foo bar baz
0 21 37 53
1 36 42 76
2 24 56 83
3 17 32 45
选项2 pd.DataFrame pd.DataFrame([d.set_in…
什么似乎是问题? 这有效: import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(2000,784))
panel = pd.Panel(data.values.reshape(2000, 28, 28))
In [49]: q.panel[42].shape
Out[49]: (28, 28)
In [51]: q.panel
Out[51]:
我认为你需要Panel.to_frame用于MultiIndex DataFrame : #with random data
df = h.to_frame()
print (df)
Adj Close Close High Low Open Volume
major minor
20…
1)reshape2创建所有年份的网格g并交叉id值并用frame对其进行rbind 。 然后使用reshape2包装cast frame从长到宽的形式,然后将其melt回长形。 最后根据需要重新排列行和列。 以#结尾的行只是为了确保每年都存在,所以如果我们知道那些情况可以省略。 以##结尾的行只是为了重新排列行和列,所以如果无关紧要,也可以省略该行。 library(reshape2)
如何从DataFrame的字典中创建Panel? In [10]: dd = {}
In [11]: for i in range(1, 3):
….: name = ‘X’ + str(i)
….: dd[name] = DataFrame(np.random.randn(3,3))
….:
In [12]: Panel(dd)
Out[12]:
Dimensions…
尝试简单地调用setSizeFull(); //如果您在该方法中实现View并查看它是否有效。 Try calling simply setSizeFull(); // if you are implementing View in that method and see if it works.
像这样的东西(需要R库reshape2和lubridate )。 # Your sample data
qqqq141101
qqqq141102
frame
Wide to long dataframe
require(reshape2);
熊猫正在尝试访问w1作为数据框,但它是一个整数。 所以当然它没有形状属性 你应该写d [w1] = df d = dict()
w0 = 50
for w1 in range(0, 10):
data[‘height_error’] = data.apply(lambda row: sq_error(w0,w1,row[‘Weight’],row[‘Height’]), axis=1)
l = df[[‘height_error’,’Weight’]].values.tolist(…
您可以使用Bootstrap创建面板
Original: https://blog.csdn.net/weixin_39595931/article/details/114389540
Author: weixin_39595931
Title: python panel dataframe_从DataFrame创建面板(Create Panel from DataFrame)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/679861/
转载文章受原作者版权保护。转载请注明原作者出处!