如何从字典列中提取数据?

发布于 2025-02-07 00:07:37 字数 888 浏览 0 评论 0原文

我有两个列的数据框,日期和详细信息。我的详细信息列Cantinas词典在每个单元格中,我必须从该字典中获取两个值,然后将其存储在其自己的单元格列中。

Date    Details
6/14/2022   {"number":"1","name":"John"}
6/15/2022   {"number":"2","name":"Steve"}
6/16/2022   {"number":"3","name":"Mira"}
6/17/2022   {"number":"17","name":"John"}
6/18/2022   {"number":"4","name":"Larry"}
6/19/2022   {"number":"33","name":"Bob"}
6/20/2022   {"number":"43","name":"james"}
6/21/2022   {"number":"112","name":"Joe"}

因此,我需要获取的是列框,其中列日期为原样,列号,从详细信息字典中提取的数字和列名提取的数字以及从详细信息词典中提取的名称。

我尝试使用这些方法: 1。

df['Name'] = list(df['Details'].keys())[0]

这产生了带有所有零的列名,这是错误的

2。

df['Name'] = df['Details'].apply(lambda x: x.get([0]))

此方法给了我错误:attributeError:'str'对象没有属性'get'3

df['Name'] = df['Details'].get([0])

这给我nan

关于我应该如何解决这个问题吗?

I have dataframe with two columns, date and Details. My details column cantinas dictionary in every cell and i have to get two values from this dictionary and store it in its own cell column.

Date    Details
6/14/2022   {"number":"1","name":"John"}
6/15/2022   {"number":"2","name":"Steve"}
6/16/2022   {"number":"3","name":"Mira"}
6/17/2022   {"number":"17","name":"John"}
6/18/2022   {"number":"4","name":"Larry"}
6/19/2022   {"number":"33","name":"Bob"}
6/20/2022   {"number":"43","name":"james"}
6/21/2022   {"number":"112","name":"Joe"}

So what i need to get is dataframe with column Date as it is, column Number with numbers extracted from Details dictionary and column Name with names extracted from the Details dictionary.

I tried using these methods:
1.

df['Name'] = list(df['Details'].keys())[0]

This produces column names with all zeros in it which is wrong

2.

df['Name'] = df['Details'].apply(lambda x: x.get([0]))

This approach gives me error: AttributeError: 'str' object has no attribute 'get'

3.

df['Name'] = df['Details'].get([0])

This give me NaN

Any suggestions on how should i go about this?

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

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

发布评论

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