英文:
Make a table from dictionares
问题
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))
# Create an empty DataFrame to store the unpacked awards
awards_df = pd.DataFrame()
# Loop through each player's awards dictionary
for index, row in players_personal.iterrows():
awards_dict = row['awards']
player_name = row['player_name']
# Loop through the awards and their respective years
for year, awards_list in awards_dict.items():
for award in awards_list:
# Create a column with the award name and set the year as its value
awards_df.at[index, award.lower().replace(' ', '_')] = year
# Merge the unpacked awards DataFrame with the original player DataFrame
result_df = pd.merge(players_personal, awards_df, left_index=True, right_index=True)
# Drop the 'awards' column since it's no longer needed
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
去掉。
import pandas as pd
dat1 = [{'player_name': row.player_name, 'award': award, 'date': date}
for row in df.itertuples()
for date, awards in row.awards.items() for award in awards]
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
<details>
<summary>英文:</summary>
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
import pandas as pd
dat1 = [{'player_name':row.player_name, 'award': award, 'date': date}
for row in df.itertuples()
for date, awards in row.awards.items() for award in awards]
pd.DataFrame(dat1).pivot('player_name', 'award', 'date').T
player_name Rafael Khakimov Sergei Belov
award
MHL All-Star Game 2010-2011 NaN
MHL Best GAA (1.79) 2010-2011 NaN
MHL Best Goaltender 2010-2011 NaN
MHL Goaltender of the Month (February) 2010-2011 NaN
MHL Goaltender of the Month (September) 2010-2011 NaN
Russia2 Silver Medal NaN 2009-2010
VHL Goaltender of the Month (November) 2021-2022 NaN
VHL Playoffs Best GAA (1.39) 2017-2018 NaN
VHL Silver Medal NaN 2013-2014
</details>
# 答案2
**得分**: 0
另一个版本:
```py
df['awards'] = df['awards'].apply(lambda x: {v[0]: k for k, v in x.items()})
df = pd.concat([df, df.pop('awards').apply(pd.Series)], axis=1)
print(df)
打印输出:
player_name Russia2 Silver Medal VHL Silver Medal MHL All-Star Game VHL Playoffs Best GAA (1.39) VHL Goaltender of the Month (November)
0 Sergei Belov 2009-2010 2013-2014 NaN NaN NaN
1 Rafael Khakimov NaN NaN 2010-2011 2017-2018 2021-2022
英文:
Another version:
df['awards'] = df['awards'].apply(lambda x: {v[0]: k for k, v in x.items()})
df = pd.concat([df, df.pop('awards').apply(pd.Series)], axis=1)
print(df)
Prints:
player_name Russia2 Silver Medal VHL Silver Medal MHL All-Star Game VHL Playoffs Best GAA (1.39) VHL Goaltender of the Month (November)
0 Sergei Belov 2009-2010 2013-2014 NaN NaN NaN
1 Rafael Khakimov NaN NaN 2010-2011 2017-2018 2021-2022
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论