pickle 格式的数据未加载到 DataFrame 中

发布于 2025-01-17 08:04:04 字数 1280 浏览 1 评论 0原文

我得到了这段代码,其中的数据以 pickle 格式存储,我正在尝试将其解开。成功运行后,它返回以下输出: <__main__.Container at 0x232ac6bde80>,但我希望将数据加载到 DataFrame 中以进行进一步操作。我在 StackOverflow 中搜索了可能的解决方案,但没有成功。请帮忙。这是代码:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
impor numpy as np
import tpickle
import skill_metrics as sm
from sys import version_info

def load_obj(name):
    # Load object from file in pickle format
    if version_info[0] == 2:
        suffix = 'pkl'
    else:
        suffix = 'pkl3'

    with open(name + '.' + suffix, 'rb') as f:
        return pickle.load(f) # Python2 succeeds

class Container(object): 
    
    def __init__(self, pred1, pred2, pred3, ref):
        self.pred1 = pred1
        self.pred2 = pred2
        self.pred3 = pred3
        self.ref = ref
        
if __name__ == '__main__':

    # Set the figure properties (optional)
    rcParams["figure.figsize"] = [8.0, 6.4] #works
#     rcParams['lines.linewidth'] = 2 # line width for plots
    rcParams.update({'font.size': 12}) # font size of axes text

    # Close any previously open graphics windows
    plt.close("all")
        
    # Read data from pickle file
    load_obj('target_data')
    
df = pd.read_pickle('target_data.pkl')
df

谢谢。

I got this code that has data stored in pickle format, I am trying to unpickle it. After it successfully ran, it was returning this output: <__main__.Container at 0x232ac6bde80>, but I want the data loaded into the DataFrame for further manipulations. I have searched StackOverflow for possible solutions to no avail. Kindly help. Here is the code:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
impor numpy as np
import tpickle
import skill_metrics as sm
from sys import version_info

def load_obj(name):
    # Load object from file in pickle format
    if version_info[0] == 2:
        suffix = 'pkl'
    else:
        suffix = 'pkl3'

    with open(name + '.' + suffix, 'rb') as f:
        return pickle.load(f) # Python2 succeeds

class Container(object): 
    
    def __init__(self, pred1, pred2, pred3, ref):
        self.pred1 = pred1
        self.pred2 = pred2
        self.pred3 = pred3
        self.ref = ref
        
if __name__ == '__main__':

    # Set the figure properties (optional)
    rcParams["figure.figsize"] = [8.0, 6.4] #works
#     rcParams['lines.linewidth'] = 2 # line width for plots
    rcParams.update({'font.size': 12}) # font size of axes text

    # Close any previously open graphics windows
    plt.close("all")
        
    # Read data from pickle file
    load_obj('target_data')
    
df = pd.read_pickle('target_data.pkl')
df

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文