已弃用的Pandas的替代方案是什么? [英] What is the alternative for the deprecated Pandas.Panel

查看:641
本文介绍了已弃用的Pandas的替代方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未来警告: 面板已弃用,在以后的版本中将被删除. 推荐的表示此类3维数据类型的方法是通过Panel.to_frame()方法在DataFrame上使用MultiIndex.

FutureWarning: Panel is deprecated and will be removed in a future version. The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method.

每当我运行此代码时,我都会收到上述错误! difference = pd.Panel(dict(df1=df1,df2=df2)) 谁能告诉我以上代码行使用Panel的另一种方法.

I am getting the above error whenever i ran this code! difference = pd.Panel(dict(df1=df1,df2=df2)) Can anyone please tell me the alternative way for usage of Panel with the above line of code.

-

def report_diff(x):
   return x[0] if x[0] == x[1] else '{} ---> {}'.format(*x)

difference = pd.Panel(dict(df1=df1,df2=df2))
res = difference.apply(report_diff, axis=0)

此处df1和df2包含分类数据和数字数据. 只是在这里比较两个数据框,以获取两者之间的差异.

Here df1 and df2 contains both categorical and numerical data. Just comparing the two dataframes here to get the differences between the two.

推荐答案

文档中中所述,建议的熊猫面板替代品使用multindex或xarray库.

As stated in the docs, the recommended replacements for a Pandas Panel are using a multindex, or the xarray library.

对于您的特定用例,此代码有点怪异,可为您带来相同的结果:

For your specific use case, this somewhat hacky code gets you the same result:

a = df1.values.reshape(df1.shape[0] * df1.shape[1])
b = df2.values.reshape(df2.shape[0] * df2.shape[1])
res = np.array([v if v == b[idx] else str(v) + '--->' + str(b[idx]) for idx, v in enumerate(a)]).reshape(
    df1.shape[0], df1.shape[1])
res = pd.DataFrame(res, columns=df1.columns)

这篇关于已弃用的Pandas的替代方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆