I'm trying to use Beanie odm query results to create a dataframe using pandas but it is creating the columns names from 0 to 76 instead of using the names that is used on the database (mongodb).
this is the code that i'm using:
async def champion_classification_generator():
data = await MatchParticipantsSchema.find({}).to_list()
if data:
df = pd.DataFrame(list(data))
print(df.head(5))
return None
how can i resolve this? already search on the documentation and didnt find any usefull information or at least didnt saw any.thanks for the help
sample of the data:
[MatchParticipantsSchema(id=ObjectId('63cca053d8b828d7da370ca4'), revision_id=None, matchID='6139692722', summonerPuuid='lb_jUxQcHM028sGKWEmnzYMtp3RmTt3op_L3vbtHJQr58l7zyyVDVPonpvulBf6npVhAraJHaYgDkA', teamSide='blue', position='TOP_LANE', win=1, championID=113, championName='Sejuani', championLevel=18, summoner_spell_d=4, summoner_spell_f=12, grade=1.33, kda=2.0, kills=5, deaths=10, assists=15, doublekills=0, triplekills=0, quadrakills=0, pentakills=0, firstblood=0, firstblood_assist=0, damage_dealt_to_champions=27959, damage_per_minute=681.93, damage_taken=52892, damage_taken_per_minute=1290.05, damage_mitigated=93914, damage_mitigated_per_minute=2290.59, wards_placed=19, wards_per_minute=0.46, wards_killed=2, control_wards_bought=4, vision_score=48, vision_score_per_minute=1.17, gold_earned=14406, gold_earned_per_minute=351.37, gold_spent=12900, total_cs=232, cs_per_minute=5.66, largest_killing_spree=2, killing_spree=1, longest_time_spent_living=480.0, magic_damage_dealt=70254, physical_damage_dealt=125120, true_damage_dealt=10658, largest_critical_strike=0, magic_damage_dealt_to_champions=13104, true_damage_dealt_to_champions=2226, total_heal=6016, damage_dealt_to_objectives=10447, damage_dealt_to_turrets=4178, time_ccing_others=53.0, turret_kills=1, inhibitor_kills=1, total_time_cc_dealt=2252.0, cs_5=24.0, xp_5=1711.0, lvl_5=5.0, gold_5=1403.0, kill_5=0, deaths_5=2, assists_5=0, cs_10=52.0, xp_10=4443.0, lvl_10=9.0, gold_10=2894.0, kill_10=1, deaths_10=4, assists_10=0, cs_15=95.0, xp_15=7026.0, lvl_15=11.0, gold_15=4389.0, kill_15=1, deaths_15=4, assists_15=1),MatchParticipantsSchema(id=ObjectId('63cca053d8b828d7da370ca5'), revision_id=None, matchID='6139692722', summonerPuuid='MhdKP3yOP9iX3x3QBdzhLKblkWkAq7iweb-OYgBW-F3Nbql4a8e96Ofl_8ViqhHtwtcWrWuQ6ID-WQ', teamSide='blue', position='JUNGLE', win=1, championID=254, championName='Vi', championLevel=18, summoner_spell_d=11, summoner_spell_f=4, grade=-1.72, kda=3.17, kills=8, deaths=6, assists=11, doublekills=0, triplekills=0, quadrakills=0, pentakills=0, firstblood=0, firstblood_assist=0, damage_dealt_to_champions=22938, damage_per_minute=559.46, damage_taken=40658, damage_taken_per_minute=991.66, damage_mitigated=75099, damage_mitigated_per_minute=1831.68, wards_placed=6, wards_per_minute=0.15, wards_killed=5, control_wards_bought=7, vision_score=36, vision_score_per_minute=0.88, gold_earned=15969, gold_earned_per_minute=389.49, gold_spent=14125, total_cs=182, cs_per_minute=4.44, largest_killing_spree=5, killing_spree=2, longest_time_spent_living=948.0, magic_damage_dealt=7902, physical_damage_dealt=171734, true_damage_dealt=22906, largest_critical_strike=0, magic_damage_dealt_to_champions=0, true_damage_dealt_to_champions=2863, total_heal=14690, damage_dealt_to_objectives=29034, damage_dealt_to_turrets=521, time_ccing_others=44.0, turret_kills=0, inhibitor_kills=0, total_time_cc_dealt=413.0, cs_5=24.0, xp_5=1311.0, lvl_5=5.0, gold_5=1489.0, kill_5=0, deaths_5=0, assists_5=0, cs_10=52.0, xp_10=3142.0, lvl_10=7.0, gold_10=2787.0, kill_10=0, deaths_10=0, assists_10=0, cs_15=78.0, xp_15=5590.0, lvl_15=10.0, gold_15=4758.0, kill_15=2, deaths_15=0, assists_15=0)]
CodePudding user response:
Without sample data, it's hard to find a solution. You have to provide a Minimal Reproducible Example.
However if your data
variable is like (a list of lists of tuples)
data = [[('id', 63), ('revision_id', None), ('matchID', 12)],
[('id', 64), ('revision_id', None), ('matchID', 13)]]
You could use the following code to get the expected result:
df = pd.DataFrame([dict(row) for row in data])
print(df)
# Output
id revision_id matchID
0 63 None 12
1 64 None 13
Update
You can use the __dict__
attribute on your MatchParticipantsSchema
instances:
df = pd.DataFrame([i.__dict__ for i in data])