【问题标题】:how to create a dataframe from saved data, calculated in a function如何从保存的数据创建数据框,在函数中计算
【发布时间】:2021-01-24 18:42:20
【问题描述】:

您好,我有这些嵌套字典和列表作为我的数据。这是两场比赛的摘要。

 my_stats = [
 {'_id': 'GLEvHIL2020031419A', 'stats': 
 {'data': [
 {'area': 'B1', 'key': 'Lineouts', 'possession': 'against', 'second': 7, 'endSecond': 27, 
 'time': '2020-03-14T12:00:43', 'Lineout Presentation': 'Scrappy', 'Lineout Errors': 'Lost'},
 {'area': 'D2', 'key': 'Lineouts', 'possession': 'for', 'second': 344, 'endSecond': 383, 'time': 
  '2020-03-14T12:53:43', 'Lineout Presentation': 'Goodball', 'Lineout Errors': 'Lost'},
 {'area': 'A1', 'key': 'Lineouts', 'possession': 'for', 'second': 354, 'endSecond': 358, 'time': 
  '2020-03-14T12:56:43', 'Lineout Errors': 'Lost'}]}},
 {'_id': 'HJSvMON2020031419A', 'stats': 
 {'data': 
 [{'area': 'C1', 'key': 'Kick Off', 'possession': 'against', 'second': 0, 'endSecond': 6, 'time': 
 '2020-03-14T12:00:06', 'Kick Off Fielded': 'Unsuccessful'},
 {'key': 'Passive Tackle', 'possession': 'against', 'second': 9, 'time': '2020-03-14T12:00:09', 
 'value': 9, 'subMetric': 3, 'rtp': []}, 
 {'area': 'D1', 'key': 'Rucks', 'possession': 'against', 'second': 9, 'endSecond': 19, 'time': '2020- 
  03-14T12:00:19'}, 
 {'area': 'D1', 'key': 'Lineouts', 'possession': 'for', 'second': 33, 'endSecond': 45, 'time': '2020- 
 03-14T12:00:45', 'Lineout Formations': '5 Man', 'Lineout Area Of Throw': 'Front', 'Lineout 
 Contested': 'Contested', 'Lineout Presentation': 'Scrappy', 'Lineout Errors': 'Lost', 'Maul Meters': 
 '5'}, 
 {'key': 'Neutral Tackle', 'possession': 'for', 'second': 45, 'time': '2020-03-14T12:00:45', 'value': 
 1, 'subMetric': 2, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 49, 
'time': '2020-03-14T12:00:49', 'value': 1, 'subMetric': None}]},
 {'area': 'D1', 'key': 'Rucks', 'possession': 'for', 'second': 47, 'endSecond': 50, 'time': '2020-03- 
 14T12:00:50'}, 
 {'key': 'Defender in Position', 'possession': 'for', 'second': 49, 'time': '2020-03-14T12:00:49', 
 'value': 1, 'subMetric': None, 'rtp': []}, 
 {'key': 'Passive Tackle', 'possession': 'for', 'second': 51, 'time': '2020-03-14T12:00:51', 'value':       
 7, 'subMetric': 3, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 53, 
 'time': '2020-03-14T12:00:53', 'value': 7, 'subMetric': None}]}, 
 {'key': 'Tackle Assist', 'possession': 'for', 'second': 52, 'time': '2020-03-14T12:00:52', 'value': 
 8, 'subMetric': None, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 53, 
 'time': '2020-03-14T12:00:53', 'value': 8, 'subMetric': None}]}, 
 {'key': 'Defender in Position', 'possession': 'for', 'second': 53, 'time': '2020-03-14T12:00:53', 
 'value': 8, 'subMetric': None, 'rtp': []}]}}]

我需要编写一个函数来计算每场比赛赢得的争边球并保存游戏名称 (_id) 以及每场比赛的 Goodball、Scrappy 和 Drive 争边球的数量。为了让我创建一个能够创建图表的数据框。

第一个问题是并非所有带有 key : lineout 的字典都有一个“Lineout Presentation”键。我通过 try: except: 克服了这个问题:

Lineouts = []
Games = []
def Our_Lineouts_Won():
        global Lineouts
        Lineouts = (0)
        
        for l in my_stats:
            t=(l['stats']['data'])
            print(l['_id'])
            Games.append(str(l['_id']))
            
            for i in t:
                 
                 try:
                     if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout 
                     Presentation')])==  'Off the Top'  or 'Scrappy' or  'Drive'  )):
          
                        
                        Lineouts += 1 
                        
                 except KeyError:
                    pass  

            #print(str(Lineouts))
            Lineouts.insert(str(Lineouts)) 

            #for Lineouts in my_stats:       
                        
                 
            Lineouts.append(Lineouts)
                    
            
            
        #Lineouts = (0) 
        #return Lineouts
    
        
Our_Lineouts_Won()

print (Games) 
print (Lineouts)

我想要的结果是一个带有游戏名称 (_id) 的数据框,后跟计算数据的列(其中 Goodball 阵容将是第一个。)

我已尝试将 Lineout 添加到列表中。

请帮忙。

【问题讨论】:

  • global Lineouts; Lineouts = (0) - 那应该怎么做?
  • 感谢您的评论。全局 Lineouts 应该将数据添加到 for 循环之外。我不得不把 Lineouts = 0 让我的 for 循环在每场比赛中从 0 开始。可能会有更简单的方法来做到这一点。我对编程很陌生。

标签: python function dataframe


【解决方案1】:

我不敢相信,但我做到了。这是我设法解决的第一个(对我而言)大问题。

这里是如何。我创建了一个字典 lpg... 是每场比赛阵容的缩写。 然后我将所有 _id 附加到列表 Games 中。

在那之后,我使用了 try 和 except,如果没有捕获到 Lineout 演示文稿,则阻止代码中断。

每场比赛都会添加每个符合条件的边线并保存在变量 L1 下。

然后我用

更新了我的空字典

lpg.update({games:L1})

如您所见,我从字典中创建了数据框 df = pd.DataFrame(list (lpg.items())).

Lineouts = []
Games = []
lpg = {}
def Our_Lineouts_Won():
        global Lineouts
        global lpg
        Lineouts = (0)
        lpg = {}
        for l in my_stats:
            t=(l['stats']['data'])
            games = (l['_id'])
            Games.append(str(l['_id']))
            #print (games)
            
            for i in t:
                 
                 try:
                     if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout Presentation')])==  'Off the Top'  or 'Scrappy' or  'Drive'  )):
            #if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout Presentation')])==  'Off the Top'  or (i[('Lineout Presentation')])==  'Scrappy' or (i[('Lineout Presentation')])==  'Drive'  )):
                        
                         Lineouts += 1
                        
                 except KeyError:
                    pass  
            
            L1=(str(Lineouts))
            
            lpg.update({games:L1})
            
            
            
        
            
            Lineouts = (0)
               
            
            
        #print (lpg)   
             
       
Our_Lineouts_Won()


print (Games)
#print (Lineouts)
#print (lpg)

df = pd.DataFrame(list (lpg.items()))

print(df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多