python panel dataframe_从DataFrame创建面板(Create Panel from DataFrame)

这篇博客主要讨论了在Python中使用Pandas的Panel数据结构时遇到的问题。作者在尝试从DataFrame创建Panel时遇到了错误,错误提示为'int'对象没有属性'shape'。解决方案是将字典中的键从'df'更改为迭代变量'w1',以便Pandas能正确识别DataFrame。最终的正确代码示例如下:d[w1] = df。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从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)

g

如何从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

id

qqqq141101

qqqq141102

frame

# Wide to long dataframe

require(reshape2);

df

熊猫正在尝试访问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创建面板

//header Goes here

//Body here

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值