将带有DICT的元素转换为Pandas DataFrame

发布于 2025-02-12 19:18:02 字数 761 浏览 1 评论 0原文

我有一个列表,其中包含dict的元素,我想将其解析到Pandas DataFrame。

[
 (
  {'resource': 'Observation1', 'date': '2021-06-01 11:39:00', 'code': '15848'}, 
  {'date': '2021-04-14 00:00:00','resource': 'Observation2'}
 ),
 (
  {'resource': 'Observation1', 'date': '2011-04-01 11:39:00', 'code': '15848'}, 
  {'date': '2012-06-15 00:00:00','resource': 'Observation2'}
 ),
]

结果框架看起来如下:

x_resource      x_date                x_code   y_date                y_resource       
Observation 1   2021-06-01 11:39:00   15848    2021-04-14 00:00:00   Observation 2
Observation 1   2011-04-01 11:39:00   15848    2012-06-15 00:00:00   Observation 2

我尝试使用pd.multiindex.from_tuples,但无法处理元组中的dicts ...

关于如何解决此问题的任何建议? 感谢您的帮助!

I have a list with tuples that contain dicts which I would like to parse to a pandas dataframe.

[
 (
  {'resource': 'Observation1', 'date': '2021-06-01 11:39:00', 'code': '15848'}, 
  {'date': '2021-04-14 00:00:00','resource': 'Observation2'}
 ),
 (
  {'resource': 'Observation1', 'date': '2011-04-01 11:39:00', 'code': '15848'}, 
  {'date': '2012-06-15 00:00:00','resource': 'Observation2'}
 ),
]

The resulting dataframe would look as follows:

x_resource      x_date                x_code   y_date                y_resource       
Observation 1   2021-06-01 11:39:00   15848    2021-04-14 00:00:00   Observation 2
Observation 1   2011-04-01 11:39:00   15848    2012-06-15 00:00:00   Observation 2

I have tried to use the pd.MultiIndex.from_tuples but it can't handle the dicts within the tuples...

Any suggestion on how to solve this?
Thank you for your help!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2025-02-19 19:18:02

对我来说,这个代码工作正常。我认为解决问题对您有帮助。

import pandas as pd

data = [
    (
        {'resource': 'Observation1', 'date': '2021-06-01 11:39:00', 'code': '15848'},
        {'date': '2021-04-14 00:00:00', 'resource': 'Observation2'}
    ),
    (
        {'resource': 'Observation1', 'date': '2011-04-01 11:39:00', 'code': '15848'},
        {'date': '2012-06-15 00:00:00', 'resource': 'Observation2'}
    ),
]

df = pd.DataFrame(data, columns=['x', 'y'])
df = pd.concat([df['x'].apply(pd.Series).add_prefix('x_'), df['y'].apply(pd.Series).add_prefix('y_')], axis=1)

print(df)

For me this code is working fine. I think it would be helpful for you to solve the problem.

import pandas as pd

data = [
    (
        {'resource': 'Observation1', 'date': '2021-06-01 11:39:00', 'code': '15848'},
        {'date': '2021-04-14 00:00:00', 'resource': 'Observation2'}
    ),
    (
        {'resource': 'Observation1', 'date': '2011-04-01 11:39:00', 'code': '15848'},
        {'date': '2012-06-15 00:00:00', 'resource': 'Observation2'}
    ),
]

df = pd.DataFrame(data, columns=['x', 'y'])
df = pd.concat([df['x'].apply(pd.Series).add_prefix('x_'), df['y'].apply(pd.Series).add_prefix('y_')], axis=1)

print(df)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文