熊猫DataFrame自动将错误的值作为索引

我试图从JSON文件创建DataFrames。


我有一个名为“ Series_participants”的列表,其中包含此JSON文件的一部分。当我打印它时,我的列表看起来像这样。


participantId                                                                1

championId                                                                  76

stats                        {'item0': 3265, 'item2': 3143, 'totalUnitsHeal...

teamId                                                                     100

timeline                     {'participantId': 1, 'csDiffPerMinDeltas': {'1...

spell1Id                                                                     4

spell2Id                                                                    12

highestAchievedSeasonTier                                               SILVER

dtype: object

<class 'list'>

在我尝试将此列表转换为这样的DataFrame之后


pd.DataFrame(Series_participants)

但是,熊猫使用“统计”和“时间轴”的值作为DataFrame的索引。我希望具有自动索引范围(0,...,n)


编辑1:


   participantId    championId     stats  teamId    timeline    spell1Id  spell2Id  highestAchievedSeasonTier

0       1               76         3265     100       NaN          4          12     SILVER

我想要一个数据框,其中包含“统计”和“时间轴”列,这些列包含其值的系列,如“系列”显示中一样。


我的错误是什么?


编辑2:


我试图手动创建DataFrame,但是熊猫没有考虑我的选择,最后选择了Series的“ stats”键的索引。


这是我的代码:


for j in range(0,len(df.participants[0])):


    for i in range(0,len(df.participants[0][0])):


        Series_participants = pd.Series(df.participants[0][i])

        test = {'participantId':Series_participants.values[0],'championId':Series_participants.values[1],'stats':Series_participants.values[2],'teamId':Series_participants.values[3],'timeline':Series_participants.values[4],'spell1Id':Series_participants.values[5],'spell2Id':Series_participants.values[6],'highestAchievedSeasonTier':Series_participants.values[7]}


        if j == 0:

            df_participants = pd.DataFrame(test)


        else:

            df_participants.append(test, ignore_index=True)

双循环是解析JSON文件的所有“参与者”。




心有法竹
浏览 219回答 3
3回答

四季花海

为了提高效率,您应该在构建数据框时尝试操作数据,而不要单独操作。然而,裂开你的词典键和值可以使用的组合numpy.repeat和itertools.chain。这是一个最小的示例:df = pd.DataFrame({'A': [1, 2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'B': [{'key1': 'val0', 'key2': 'val9'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'key1': 'val1', 'key2': 'val2'}],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'C': [{'key3': 'val10', 'key4': 'val8'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'key3': 'val3', 'key4': 'val4'}]})import numpy as npfrom itertools import chainchainer = chain.from_iterablelens = df['B'].map(len)res = pd.DataFrame({'A': np.repeat(df['A'], lens),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'B': list(chainer(df['B'].map(lambda x: x.values())))})res.index = chainer(df['B'].map(lambda x: x.keys()))print(res)&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;Bkey1&nbsp; 1&nbsp; val0key2&nbsp; 1&nbsp; val9key1&nbsp; 2&nbsp; val1key2&nbsp; 2&nbsp; val2

手掌心

根据评论更新:熊猫数据框可以容纳字典,但不建议这样做。熊猫解释说,您希望每个字典键都有一个索引,然后在它们之间广播单个项目列。因此,为帮助您解决问题,我建议您将字典中的项目作为列阅读。数据帧通常用于什么并且非常擅长。示例错误是由于熊猫试图通过键值对读取字典中的错误:df&nbsp;=&nbsp;pd.DataFrame(columns=&nbsp;['a',&nbsp;'b'],&nbsp;index=['a',&nbsp;'b']) df.loc['a','a']&nbsp;=&nbsp;{'apple':&nbsp;2}退货ValueError:&nbsp;Incompatible&nbsp;indexer&nbsp;with&nbsp;Series每个jpp中的以下注释(使用构造方法时):“它们可以容纳任意类型,例如df.iat[0,&nbsp;0]&nbsp;=&nbsp;{'apple':&nbsp;2}但是,不建议以这种方式使用熊猫。”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python