【问题标题】:How to group by a common value and split into columns based on it in pandas?如何在熊猫中按一个共同值分组并根据它分成列?
【发布时间】:2021-05-05 13:44:29
【问题描述】:

我有一个数据框,它在“状态”列中具有相同的共同值。我需要将其拆分为两个不同的列及其旁边的 url。

我试过了

pd.DataFrame(df.groupby(['Labels','Pattern','Status])['Count']) 它没有按预期工作。

为了便于理解,我附上了df查询和图片。

DF

import pandas as pd

df = pd.DataFrame({'Labels': {0: 'Apple',  1: 'Apple',  2: 'Apple',  3: 'Apple',  4: 'Orange',  5: 'Orange',  6: 'Orange',  7: 'Orange',  8: 'Grapes',  9: 'Grapes'}, 'Pattern': {0: 'Red',  1: 'Red',  2: 'Green',  3: 'Green',  4: 'Good',  5: 'Good',  6: 'Bad',  7: 'Bad',  8: 'Violet',  9: 'Violet'}, 'Status': {0: 'Checked',  1: 'Not_Checked',  2: 'Checked',  3: 'Not_Checked',  4: 'Checked',  5: 'Not_Checked',  6: 'Checked',  7: 'Not_Checked',  8: 'Checked',  9: 'Not_Checked'}, 'Count': {0: 79,  1: 221,  2: 3,  3: 306,  4: 13,  5: 297,  6: 28,  7: 281,  8: 20,  9: 290}, 'Some_Link': {0: 'http://www.example.com/',  1: 'http://angle.example.com/',  2: 'https://example.com/',  3: 'http://www.example.com/www.php',  4: 'http://example.com/blow/bag',  5: 'https://www.example.com/?baby=brake',  6: 'https://www.example.org/?afternoon=approval&baseball=arithmetic',  7: 'https://example.net/babies/badge?amount=balance',  8: 'http://www.example.com/boundary/boat.aspx',  9: 'http://www.example.com/sssl.php'}, 'Some_Link2': {0: 'http://www.example.com/beef/approval',  1: 'https://www.example.com/qqa.php',  2: 'https://example.com/aswq.php',  3: 'http://www.example.com/believe/bike.php?amount=blade',  4: 'https://www.example.com/',  5: 'http://www.example.com/?beef=acoustics',  6: 'https://www.example.com/#apparatus',  7: 'https://www.example.com/asd.php',  8: 'http://basketball.example.com/bone/bedroom',  9: 'https://www.example.org/box/back'}})

实际输入

预期输出

【问题讨论】:

标签: python pandas


【解决方案1】:

DataFrame.set_indexDataFrame.unstackDataFrame.sort_index 一起使用,最后展平MultiIndex:

df = df.set_index(['Labels','Pattern','Status']).unstack().sort_index(level=1, axis=1)
df.columns = df.columns.map(lambda x: f'{x[0]}_{x[1]}')
df = df.reset_index()

print (df)
   Labels Pattern  Count_Checked  \
0   Apple   Green              3   
1   Apple     Red             79   
2  Grapes  Violet             20   
3  Orange     Bad             28   
4  Orange    Good             13   

                                   Some_Link_Checked  \
0                               https://example.com/   
1                            http://www.example.com/   
2          http://www.example.com/boundary/boat.aspx   
3  https://www.example.org/?afternoon=approval&ba...   
4                        http://example.com/blow/bag   

                           Some_Link2_Checked  Count_Not_Checked  \
0                https://example.com/aswq.php                306   
1        http://www.example.com/beef/approval                221   
2  http://basketball.example.com/bone/bedroom                290   
3          https://www.example.com/#apparatus                281   
4                    https://www.example.com/                297   

                             Some_Link_Not_Checked  \
0                   http://www.example.com/www.php   
1                        http://angle.example.com/   
2                  http://www.example.com/sssl.php   
3  https://example.net/babies/badge?amount=balance   
4              https://www.example.com/?baby=brake   

                              Some_Link2_Not_Checked  
0  http://www.example.com/believe/bike.php?amount...  
1                    https://www.example.com/qqa.php  
2                   https://www.example.org/box/back  
3                    https://www.example.com/asd.php  
4             http://www.example.com/?beef=acoustics  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 2020-12-11
    • 2021-11-18
    • 2021-09-10
    • 2021-09-30
    • 2022-11-10
    • 2022-07-14
    • 1970-01-01
    相关资源
    最近更新 更多