使用前缀重命名新的拆分列

发布于 2025-01-19 03:46:08 字数 899 浏览 0 评论 0原文

我有一个数据框,其中包含两列字典。

      type                              possession_team
0  {'id': 35, 'name': 'Starting XI'}  {'id':9101,'name':'San Diego Wave'}
1  {'id': 35, 'name': 'Starting XI'}  {'id':9101,'name':'San Diego Wave'}         
2  {'id': 18, 'name': 'Half Start'}   {'id':9101,'name':'San Diego Wave'}         
3  {'id': 18, 'name': 'Half Start'}   {'id':9101,'name':'San Diego Wave'}        
4  {'id': 30, 'name': 'Pass'}         {'id':9101,'name':'San Diego Wave'}

pd.concat([df, df['type'].apply(pd.Series)], axis = 1).drop('type', axis = 1)

通常会立即手动拆分列。我将如何使用此代码,同时向它创建的结果列添加前缀?前缀是它创建的结果列的前缀,所以我会有;

     type_id type_name                             
0     35    'Starting XI'
1     35    'Starting XI'         
2     18    'Half Start'        
3     18    'Half Start'   
4     30      'Pass'  

I have a dataframe, which includes two columns which are dicts.

      type                              possession_team
0  {'id': 35, 'name': 'Starting XI'}  {'id':9101,'name':'San Diego Wave'}
1  {'id': 35, 'name': 'Starting XI'}  {'id':9101,'name':'San Diego Wave'}         
2  {'id': 18, 'name': 'Half Start'}   {'id':9101,'name':'San Diego Wave'}         
3  {'id': 18, 'name': 'Half Start'}   {'id':9101,'name':'San Diego Wave'}        
4  {'id': 30, 'name': 'Pass'}         {'id':9101,'name':'San Diego Wave'}

I use

pd.concat([df, df['type'].apply(pd.Series)], axis = 1).drop('type', axis = 1)

to split the columns manually at the minute. How would I use this code, but also add a prefix to the resulting columns that it creates? The prefix being that of the resulting columns that it creates, so I would have;

     type_id type_name                             
0     35    'Starting XI'
1     35    'Starting XI'         
2     18    'Half Start'        
3     18    'Half Start'   
4     30      'Pass'  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2025-01-26 03:46:08

IIUC,并假设字典,您可以这样做:

df['type_id'] = df['type'].str['id']
df['type_name'] = df['type'].str['name']

对于更通用的方法:

for c in df['type'].explode().unique():
    df[f'type_{c}'] = df['type'].str[c]

甚至更通用(适用于所有列):

for col in ['type', 'possession_team']: # or df.columns
    for c in df[col].explode().unique():
        df[f'{col}_{c}'] = df[col].str[c]

输出:

                                type                         possession_team  \
0  {'id': 35, 'name': 'Starting XI'}  {'id': 9101, 'name': 'San Diego Wave'}   
1  {'id': 35, 'name': 'Starting XI'}  {'id': 9101, 'name': 'San Diego Wave'}   
2   {'id': 18, 'name': 'Half Start'}  {'id': 9101, 'name': 'San Diego Wave'}   
3   {'id': 18, 'name': 'Half Start'}  {'id': 9101, 'name': 'San Diego Wave'}   
4         {'id': 30, 'name': 'Pass'}  {'id': 9101, 'name': 'San Diego Wave'}   

   type_id    type_name  possession_team_id possession_team_name  
0       35  Starting XI                9101       San Diego Wave  
1       35  Starting XI                9101       San Diego Wave  
2       18   Half Start                9101       San Diego Wave  
3       18   Half Start                9101       San Diego Wave  
4       30         Pass                9101       San Diego Wave  

IIUC, and assuming dictionaries, you could do:

df['type_id'] = df['type'].str['id']
df['type_name'] = df['type'].str['name']

For a more generic approach:

for c in df['type'].explode().unique():
    df[f'type_{c}'] = df['type'].str[c]

And even more generic (apply to all columns):

for col in ['type', 'possession_team']: # or df.columns
    for c in df[col].explode().unique():
        df[f'{col}_{c}'] = df[col].str[c]

output:

                                type                         possession_team  \
0  {'id': 35, 'name': 'Starting XI'}  {'id': 9101, 'name': 'San Diego Wave'}   
1  {'id': 35, 'name': 'Starting XI'}  {'id': 9101, 'name': 'San Diego Wave'}   
2   {'id': 18, 'name': 'Half Start'}  {'id': 9101, 'name': 'San Diego Wave'}   
3   {'id': 18, 'name': 'Half Start'}  {'id': 9101, 'name': 'San Diego Wave'}   
4         {'id': 30, 'name': 'Pass'}  {'id': 9101, 'name': 'San Diego Wave'}   

   type_id    type_name  possession_team_id possession_team_name  
0       35  Starting XI                9101       San Diego Wave  
1       35  Starting XI                9101       San Diego Wave  
2       18   Half Start                9101       San Diego Wave  
3       18   Half Start                9101       San Diego Wave  
4       30         Pass                9101       San Diego Wave  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文