如何从字典列中提取数据?
我有两个列的数据框,日期和详细信息。我的详细信息列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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论