传递值的嵌套字典数据帧错误形状为(13,1),索引暗示(13,13)

2024-06-23 03:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个嵌套字典,需要帮助创建数据框

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…)

非常感谢你的帮助


Tags: columnsinidpandasvideolinemusicprops
1条回答
网友
1楼 · 发布于 2024-06-23 03:30:29

将列字典转换为数据帧并运行以下操作:

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']

相关问题 更多 >

    热门问题