【问题标题】:Nested Dictionary Dataframe Error Shape of passed values is (13, 1), indices imply (13, 13)传递值的嵌套字典数据框错误形状为 (13, 1),索引暗示 (13, 13)
【发布时间】:2020-08-08 22:42:48
【问题描述】:

我正在处理嵌套字典,我需要有关创建数据框的帮助。

Python 代码:

authorstats= props['authorStats']
follower = props['authorStats']['followerCount']
heartcount = props['authorStats']['heartCount']
AuthorID = props['itemInfos']['authorId']
Commentcount = props['itemInfos']['commentCount']
CreateTime = props['itemInfos']['createTime']
Diggcount = props['itemInfos']['diggCount']
ID = props['itemInfos']['id']
VideoPlaycount = props['itemInfos']['playCount']
VideoSharecount = props['itemInfos']['shareCount']
VideoText = props['itemInfos']['text']
MusicAuthorName = props['musicInfos']['authorName']
MusicID = props['musicInfos']['musicId']
Musicname = props['musicInfos']['musicName']
Meta = follower,heartcount,AuthorID,Commentcount,CreateTime,Diggcount,ID,VideoPlaycount,VideoSharecount,VideoText,MusicAuthorName,MusicID,Musicname
import pandas
print(pandas.DataFrame(Meta,columns=['NumberofFollowers','HeartCount','AuthorID','NumberofComments','Create Time','Diggcount','ID','Video Play Count','Video Share Count','Video Text','Music Author Name','Music ID','Music Name']))

我得到的错误信息是

(2147278, '10899962', '84453035275386880', 300, '1587111974', 14089, '6816594007829859589', 91036, 29, 'I’m that friend who can’t stand still when taking photos', 'Alan Walker, Ruben', '6805465027164768258', 'Heading Home')

    File "C:/Users/abcd/PycharmProjects/Selinium/Lets start working", line 62, in <module>
        print(pandas.DataFrame(Meta,columns=['NumberofFollowers','HeartCount','AuthorID','NumberofComments','Create Time','Diggcount','ID','Video Play Count','Video Share Count','Video Text','Music Author Name','Music ID','Music Name']))
      File "C:\Users\abcd\PycharmProjects\Selinium\venv\lib\site-packages\pandas\core\frame.py", line 488, in __init__
        mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
      File "C:\Users\k.bengi\PycharmProjects\Selinium\venv\lib\site-packages\pandas\core\internals\construction.py", line 210, in init_ndarray
        return create_block_manager_from_blocks(block_values, [columns, index])
      File "C:\Users\abcd\PycharmProjects\Selinium\venv\lib\site-packages\pandas\core\internals\managers.py", line 1664, in create_block_manager_from_blocks
        construction_error(tot_items, blocks[0].shape[1:], axes, e)
      File "C:\Users\abcd\PycharmProjects\Selinium\venv\lib\site-packages\pandas\core\internals\managers.py", line 1694, in construction_error
        raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
    ValueError: Shape of passed values is (13, 1), indices imply (13, 13)`

预期输出是一个表,其中包含“NumberofFollowers”、“HeartCount”、“AuthorID”等列,其中包含您可以在第一行错误中看到的信息,例如 2147278 是追随者数量,“10899962”是HeartCount, '84453035275386880' 是作者 ID ...)

非常感谢您的帮助!

【问题讨论】:

    标签: python dataframe dictionary nested tuples


    【解决方案1】:

    将列字典转换为 DataFrame 并运行以下命令:

    df = pandas.concat([follower,heartcount,AuthorID,Commentcount,CreateTime,Diggcount,ID,VideoPlaycount,VideoSharecount,VideoText,MusicAuthorName,MusicID,Musicname])
    

    应该正确创建DataFrame,只有列需要重命名:

    df.columns=['NumberofFollowers','HeartCount','AuthorID','NumberofComments','Create Time','Diggcount','ID','Video Play Count','Video Share Count','Video Text','Music Author Name','Music ID','Music Name']
    

    【讨论】:

    • 我是 python 中的新手 :) 尝试使用您的代码并得到此错误:无法连接类型为 '' 的对象;只有 Series 和 DataFrame obj 是有效的。如何将列字典转换为数据框?
    • 您可以使用follower = pandas.DataFrame(list(follower.items())) (以及分别用于其他字典)。这会将键和值放在不同的列中,这是您要查找的内容吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 2020-05-21
    • 2021-12-05
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多