熊猫面板的深层副本?

问题描述 投票:0回答:1

当我尝试使用instructions provided in the online documentation复制一个pandas面板对象时,我没有得到预期的行为。

也许这会说明问题:

import pandas as pd

# make first panel with some bogus numbers 
dates = pd.date_range('20130101',periods=6)
df1 = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
df2 = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('EFGH'))
pnl = {} 
pnl['alpha'] = df1
pnl['beta'] = df2

# copy pnl into pnl2
# according to online docs the default is 'deep=True'
# but it chokes when I try to specify deep=True 
pnl2 = pnl.copy()


# now delete column C from pnl2['alpha']
del pnl2['alpha']['C']



#Now when I try to find column C in the original panel (pnl) it's gone! 

我认为必须有一个光滑的解决方案,但我无法在在线文档中找到它,也没有在Wes McKinney的书中找到它(我唯一的关于熊猫的书......)。

任何提示/建议非常感谢!

pandas deep-copy
1个回答
1
投票

你没有制作一个Panel,只是一个DataFrames的词典。添加此行将其转换为Panel对象,它应该按预期工作。

pnl = pd.Panel(pnl)
© www.soinside.com 2019 - 2024. All rights reserved.