制作一个表格,使用字典。

huangapple go评论113阅读模式
英文:

Make a table from dictionares

问题

player_name russia2_silver_medal mhl_allstar_game
Sergei Belov 2009-2010 NaN
Rafael Khakimov NaN 2010-2011
  1. players_personal['awards'] = players_personal['awards'].apply(lambda x: json.loads(x))
  2. # Create an empty DataFrame to store the unpacked awards
  3. awards_df = pd.DataFrame()
  4. # Loop through each player's awards dictionary
  5. for index, row in players_personal.iterrows():
  6. awards_dict = row['awards']
  7. player_name = row['player_name']
  8. # Loop through the awards and their respective years
  9. for year, awards_list in awards_dict.items():
  10. for award in awards_list:
  11. # Create a column with the award name and set the year as its value
  12. awards_df.at[index, award.lower().replace(' ', '_')] = year
  13. # Merge the unpacked awards DataFrame with the original player DataFrame
  14. result_df = pd.merge(players_personal, awards_df, left_index=True, right_index=True)
  15. # Drop the 'awards' column since it's no longer needed
  16. result_df = result_df.drop(columns=['awards'])

This code will help you unpack the awards into separate columns in the DataFrame, where each award corresponds to the player's name and the year in which they received it.

英文:
player_name awards
Sergei Belov {'2009-2010': ['Russia2 Silver Medal'], '2013-2014': ['VHL Silver Medal']}
Rafael Khakimov {'2010-2011': ['MHL All-Star Game','MHL Best GAA (1.79)','MHL Best Goaltender','MHL Goaltender of the Month (February)','MHL Goaltender of the Month (September)'],'2017-2018': ['VHL Playoffs Best GAA (1.39)'], '2021-2022': ['VHL Goaltender of the Month (November)']}

How to unpack awards in python into the column of the name of the awards and into the value of the year of the season when it was received.

Sample result:

player_name russia2_silver_medal mhl_allstar_game
Sergei Belov 2009-2010 NaN
Rafael Khakimov NaN 2010-2011

players_personal['awards'] = players_personal['awards'].apply(lambda x: json.loads(x))

First, I converted from strings to json, and then tried to collect them into a dataframe through a loop, but not successfully

答案1

得分: 0

这是一种方法。请注意,我使用了T来转置数据以在此处显示。您可以在最后将T去掉。

  1. import pandas as pd
  2. dat1 = [{'player_name': row.player_name, 'award': award, 'date': date}
  3. for row in df.itertuples()
  4. for date, awards in row.awards.items() for award in awards]
  5. pd.DataFrame(dat1).pivot('player_name', 'award', 'date').T

玩家名称 Rafael Khakimov Sergei Belov
奖项
MHL全明星赛 2010-2011 NaN
MHL最佳GAA(1.79) 2010-2011 NaN
MHL最佳守门员 2010-2011 NaN
MHL本月最佳守门员(二月) 2010-2011 NaN
MHL本月最佳守门员(九月) 2010-2011 NaN
俄罗斯2银牌 NaN 2009-2010
VHL本月最佳守门员(十一月) 2021-2022 NaN
VHL季后赛最佳GAA(1.39) 2017-2018 NaN
VHL银牌 NaN 2013-2014

  1. <details>
  2. <summary>英文:</summary>
  3. Here is one way. Note that I `T` transposed the data to make it visible here. You can get rid of the `T` at the very end
  4. import pandas as pd
  5. dat1 = [{&#39;player_name&#39;:row.player_name, &#39;award&#39;: award, &#39;date&#39;: date}
  6. for row in df.itertuples()
  7. for date, awards in row.awards.items() for award in awards]
  8. pd.DataFrame(dat1).pivot(&#39;player_name&#39;, &#39;award&#39;, &#39;date&#39;).T
  9. player_name Rafael Khakimov Sergei Belov
  10. award
  11. MHL All-Star Game 2010-2011 NaN
  12. MHL Best GAA (1.79) 2010-2011 NaN
  13. MHL Best Goaltender 2010-2011 NaN
  14. MHL Goaltender of the Month (February) 2010-2011 NaN
  15. MHL Goaltender of the Month (September) 2010-2011 NaN
  16. Russia2 Silver Medal NaN 2009-2010
  17. VHL Goaltender of the Month (November) 2021-2022 NaN
  18. VHL Playoffs Best GAA (1.39) 2017-2018 NaN
  19. VHL Silver Medal NaN 2013-2014
  20. </details>
  21. # 答案2
  22. **得分**: 0
  23. 另一个版本:
  24. ```py
  25. df['awards'] = df['awards'].apply(lambda x: {v[0]: k for k, v in x.items()})
  26. df = pd.concat([df, df.pop('awards').apply(pd.Series)], axis=1)
  27. print(df)

打印输出:

  1. player_name Russia2 Silver Medal VHL Silver Medal MHL All-Star Game VHL Playoffs Best GAA (1.39) VHL Goaltender of the Month (November)
  2. 0 Sergei Belov 2009-2010 2013-2014 NaN NaN NaN
  3. 1 Rafael Khakimov NaN NaN 2010-2011 2017-2018 2021-2022
英文:

Another version:

  1. df[&#39;awards&#39;] = df[&#39;awards&#39;].apply(lambda x: {v[0]: k for k, v in x.items()})
  2. df = pd.concat([df, df.pop(&#39;awards&#39;).apply(pd.Series)], axis=1)
  3. print(df)

Prints:

  1. player_name Russia2 Silver Medal VHL Silver Medal MHL All-Star Game VHL Playoffs Best GAA (1.39) VHL Goaltender of the Month (November)
  2. 0 Sergei Belov 2009-2010 2013-2014 NaN NaN NaN
  3. 1 Rafael Khakimov NaN NaN 2010-2011 2017-2018 2021-2022

huangapple
  • 本文由 发表于 2023年7月7日 00:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定