我试图从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)
但是pandas使用“stats”和“timeline”的值作为DataFrame的索引 . 我希望有自动索引范围(0,...,n)
编辑1:
participantId championId stats teamId timeline spell1Id spell2Id highestAchievedSeasonTier
0 1 76 3265 100 NaN 4 12 SILVER
我希望有一个带有“stats”和“timeline”colomns的数据框,其中包含其值的序列,如系列显示中所示 .
我的错误是什么?
编辑2:
我试图手动创建DataFrame,但是pandas没有考虑我的选择,最后获取了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文件的所有“参与者” .
最后编辑:
我用以下代码实现了我想要的东西:
for i in range(0,len(df.participants[0])):
Series_participants = pd.Series(df.participants[0][i])
df_test = pd.DataFrame(data=[Series_participants.values], columns=['participantId','championId','stats','teamId','timeline','spell1Id','spell2Id','highestAchievedSeasonTier'])
if i == 0:
df_participants = pd.DataFrame(df_test)
else:
df_participants = df_participants.append(df_test, ignore_index=True)
print(df_participants)
感谢大家的帮助 !
3 回答
如果您尝试将包含dicts的列表,系列或数组输入到对象构造函数中,则它无法识别您要执行的操作 . 解决此问题的一种方法是手动设置:
请注意,只有在您的DataFrame中已创建列 and 索引时,上述操作才有效 .
每条评论更新:Pandas数据框可以包含字典,但不建议使用 .
Pandas解释为每个字典键需要一个索引,然后在它们之间广播单个项目列 .
因此,为了帮助您尝试做什么,我建议您阅读字典项目作为列 . 这是什么数据帧通常用于和非常擅长 .
示例由于pandas尝试按键,值对读入字典而导致的错误:
回报
在下面的注释中每个jpp(当使用构造函数方法时):
“它们可以容纳任意类型,例如:
但是,不建议以这种方式使用熊猫 . “
为了提高效率,您应该在构建数据框时尝试操作数据,而不是单独执行操作 .
但是,要拆分字典键和值,可以使用
numpy.repeat
和itertools.chain
的组合 . 这是一个最小的例子: