【发布时间】:2021-09-07 09:18:39
【问题描述】:
我的数据集是这样的
url boolean details
numberOfPages date
xzy.com 0 {'https://www.eltako.depdf': {'numberOfPages': 440, 'date': '2017-09-20'},'https://new.com': {'numberOfPages': 240, 'date': '2017-09-20'} }
numberOfPages 和 date col 最初是空的,而 details col 有一个字典。我想遍历所有行(url)并检查他们的details 列。对于详细信息列中的每个键,我想创建一个单独的行,然后使用 numberOfPages 和日期值来添加列值。结果应该是这样的:
url boolean pdfLink numberOfPages date
xzy.com 0 https://www.eltako.depdf 440 2017-09-20
https://new.com 240 2017-09-20
我试过了,但第二行给了我一个错误:TypeError: string indices must be integers
def arrange(df):
df=df.explode('details').reset_index(drop=True)
out=pd.DataFrame(df['details'].map(lambda x:[x[y] for y in x]).explode().tolist())
【问题讨论】:
-
因为details列里面的字典其实是字符串而不是字典
标签: python pandas dataframe dictionary data-science