converting a dictionary of nested lists into a row in a data frame

I have some data (shown below), which is a dictionary of nested lists of dictionaries.
I want to make the whole dictionary into one row. A very wide row.
At present I can get my desired result. It is quite long and not very elegant.
I’d like to get better at writing more concise code, and hopefully more computationally efficient code.

For context I am doing this for a university project. We aren’t getting marked on code elegance or optimisation. This is more for my own benefit.

Being still quite new to python I am not great at working with a sort of nested dictionary of dictionaries and lists of nested dictionaries.
Any help would be much appreciated!

Data dictionary

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{'attack': [{'stat': 'carries_crossed_gain_line', 'value': '52'},
{'stat': 'carries_metres', 'value': '648'},
{'stat': 'carries_not_made_gain_line', 'value': '64'},
{'stat': 'clean_breaks', 'value': '21'},
{'stat': 'defenders_beaten', 'value': '25'},
{'stat': 'offload', 'value': '16'},
{'stat': 'passes', 'value': '168'},
{'stat': 'runs', 'value': '138'},
{'stat': 'turnovers_conceded', 'value': '16'}],
'defence': [{'stat': 'missed_tackles', 'value': '32'},
{'stat': 'tackles', 'value': '125'},
{'stat': 'turnovers_won', 'value': '6'}],
'discipline': [{'stat': 'penalties_conceded', 'value': '12'},
{'stat': 'red_card_second_yellow', 'value': '0'},
{'stat': 'red_cards', 'value': '0'},
{'stat': 'yellow_cards', 'value': '0'}],
'kicking': [{'stat': 'conversion_goals', 'value': '3'},
{'stat': 'kicks_from_hand', 'value': '10'},
{'stat': 'missed_conversion_goals', 'value': '1'},
{'stat': 'missed_penalty_goals', 'value': '0'},
{'stat': 'penalty_goals', 'value': '2'}],
'breakdown': [{'stat': 'mauls_lost', 'value': '0'},
{'stat': 'mauls_total', 'value': '6'},
{'stat': 'mauls_won', 'value': '6'},
{'stat': 'mauls_won_penalty', 'value': '1'},
{'stat': 'mauls_won_try', 'value': '0'},
{'stat': 'rucks_lost', 'value': '11'},
{'stat': 'rucks_total', 'value': '99'},
{'stat': 'rucks_won', 'value': '88'}],
'lineouts': [{'stat': 'lineout_success', 'value': '0.93'},
{'stat': 'lineout_won_steal', 'value': '1'},
{'stat': 'lineouts_Lost', 'value': '1'},
{'stat': 'lineouts_won', 'value': '14'}],
'scrums': [{'stat': 'scrums_lost', 'value': '0'},
{'stat': 'scrums_success', 'value': '1.00'},
{'stat': 'scrums_won', 'value': '2'}],
'possession': [{'stat': 'possession', 'value': '0.50'},
{'stat': 'pc_possession_first', 'value': '0.50'},
{'stat': 'pc_possession_second', 'value': '0.50'},
{'stat': 'ball_possession_last_10_mins', 'value': '0.61'}]}
</code>
<code>{'attack': [{'stat': 'carries_crossed_gain_line', 'value': '52'}, {'stat': 'carries_metres', 'value': '648'}, {'stat': 'carries_not_made_gain_line', 'value': '64'}, {'stat': 'clean_breaks', 'value': '21'}, {'stat': 'defenders_beaten', 'value': '25'}, {'stat': 'offload', 'value': '16'}, {'stat': 'passes', 'value': '168'}, {'stat': 'runs', 'value': '138'}, {'stat': 'turnovers_conceded', 'value': '16'}], 'defence': [{'stat': 'missed_tackles', 'value': '32'}, {'stat': 'tackles', 'value': '125'}, {'stat': 'turnovers_won', 'value': '6'}], 'discipline': [{'stat': 'penalties_conceded', 'value': '12'}, {'stat': 'red_card_second_yellow', 'value': '0'}, {'stat': 'red_cards', 'value': '0'}, {'stat': 'yellow_cards', 'value': '0'}], 'kicking': [{'stat': 'conversion_goals', 'value': '3'}, {'stat': 'kicks_from_hand', 'value': '10'}, {'stat': 'missed_conversion_goals', 'value': '1'}, {'stat': 'missed_penalty_goals', 'value': '0'}, {'stat': 'penalty_goals', 'value': '2'}], 'breakdown': [{'stat': 'mauls_lost', 'value': '0'}, {'stat': 'mauls_total', 'value': '6'}, {'stat': 'mauls_won', 'value': '6'}, {'stat': 'mauls_won_penalty', 'value': '1'}, {'stat': 'mauls_won_try', 'value': '0'}, {'stat': 'rucks_lost', 'value': '11'}, {'stat': 'rucks_total', 'value': '99'}, {'stat': 'rucks_won', 'value': '88'}], 'lineouts': [{'stat': 'lineout_success', 'value': '0.93'}, {'stat': 'lineout_won_steal', 'value': '1'}, {'stat': 'lineouts_Lost', 'value': '1'}, {'stat': 'lineouts_won', 'value': '14'}], 'scrums': [{'stat': 'scrums_lost', 'value': '0'}, {'stat': 'scrums_success', 'value': '1.00'}, {'stat': 'scrums_won', 'value': '2'}], 'possession': [{'stat': 'possession', 'value': '0.50'}, {'stat': 'pc_possession_first', 'value': '0.50'}, {'stat': 'pc_possession_second', 'value': '0.50'}, {'stat': 'ball_possession_last_10_mins', 'value': '0.61'}]} </code>
{'attack': [{'stat': 'carries_crossed_gain_line', 'value': '52'},
  {'stat': 'carries_metres', 'value': '648'},
  {'stat': 'carries_not_made_gain_line', 'value': '64'},
  {'stat': 'clean_breaks', 'value': '21'},
  {'stat': 'defenders_beaten', 'value': '25'},
  {'stat': 'offload', 'value': '16'},
  {'stat': 'passes', 'value': '168'},
  {'stat': 'runs', 'value': '138'},
  {'stat': 'turnovers_conceded', 'value': '16'}],
 'defence': [{'stat': 'missed_tackles', 'value': '32'},
  {'stat': 'tackles', 'value': '125'},
  {'stat': 'turnovers_won', 'value': '6'}],
 'discipline': [{'stat': 'penalties_conceded', 'value': '12'},
  {'stat': 'red_card_second_yellow', 'value': '0'},
  {'stat': 'red_cards', 'value': '0'},
  {'stat': 'yellow_cards', 'value': '0'}],
 'kicking': [{'stat': 'conversion_goals', 'value': '3'},
  {'stat': 'kicks_from_hand', 'value': '10'},
  {'stat': 'missed_conversion_goals', 'value': '1'},
  {'stat': 'missed_penalty_goals', 'value': '0'},
  {'stat': 'penalty_goals', 'value': '2'}],
 'breakdown': [{'stat': 'mauls_lost', 'value': '0'},
  {'stat': 'mauls_total', 'value': '6'},
  {'stat': 'mauls_won', 'value': '6'},
  {'stat': 'mauls_won_penalty', 'value': '1'},
  {'stat': 'mauls_won_try', 'value': '0'},
  {'stat': 'rucks_lost', 'value': '11'},
  {'stat': 'rucks_total', 'value': '99'},
  {'stat': 'rucks_won', 'value': '88'}],
 'lineouts': [{'stat': 'lineout_success', 'value': '0.93'},
  {'stat': 'lineout_won_steal', 'value': '1'},
  {'stat': 'lineouts_Lost', 'value': '1'},
  {'stat': 'lineouts_won', 'value': '14'}],
 'scrums': [{'stat': 'scrums_lost', 'value': '0'},
  {'stat': 'scrums_success', 'value': '1.00'},
  {'stat': 'scrums_won', 'value': '2'}],
 'possession': [{'stat': 'possession', 'value': '0.50'},
  {'stat': 'pc_possession_first', 'value': '0.50'},
  {'stat': 'pc_possession_second', 'value': '0.50'},
  {'stat': 'ball_possession_last_10_mins', 'value': '0.61'}]}

Desired outcome
a wide row like:
enter image description here

What I’ve tried
I made a function that I can use to call each nested DF like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def trim(df):
x = df.transpose()
x.columns = x.iloc[0]
x = x[1:]
return x
</code>
<code>def trim(df): x = df.transpose() x.columns = x.iloc[0] x = x[1:] return x </code>
def trim(df):
    x = df.transpose()
    x.columns = x.iloc[0]
    x = x[1:]
    
    return x

Which I call in this manner. altering the last ‘attack’ to the other stats:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>trim(pd.DataFrame(results['home']['team_stats']['attack']))
</code>
<code>trim(pd.DataFrame(results['home']['team_stats']['attack'])) </code>
trim(pd.DataFrame(results['home']['team_stats']['attack']))

Then I

  1. concatenate the frames into one row.
  2. Add in the ‘home’ prefix to all the columns.
  3. Repeat steps one and two for the away team, adding the prefix ‘away’ to the columns
  4. concatenate both results to a very, very wide row.
  5. Add the match details meta data to the row, making it very, very, very wide.
  6. Repeat this for every set of match data.

This is time consuming. Is there a way that I can do this in a more efficient or pythonic manner?
My end goal is to collect this match data for the home and away teams (therefore doubling the data frame width) and making one row per game.

Extra data for reference
Results: I am only interest in the [‘match’], [‘home’] and [‘away’] dictionaries.
Within the home and away dictionaries, the ‘team_stats’ part is what I am trying to extract.
This data comes from an API. I turn the response into json format first.
I will be processing one set of data like this per match. And will be aiming for around 250+ matches.

data

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật