一行拆多行参考:https://blog.csdn.net/baidu_30809315/article/details/93748732

pandas01

pandas02

def merge_rows_in_pandas():
    data = [{'name': 'jalen', 'age': 12, 'love': 'basketball'},
            {'name': 'jalen', 'age': 12, 'love': 'football'},
            {'name': 'kitty', 'age': 20, 'love': 'movies'},
            {'name': 'kitty', 'age': 20, 'love': 'singing'}]
    df = pd.DataFrame(data)
    df['love'] = df.groupby(['name', 'age'], as_index=False)['love'].transform(lambda x: ','.join(x))
    df.drop_duplicates(subset=['name', 'age'], keep='first', inplace=True)
 
 
if __name__ == '__main__':
    merge_rows_in_pandas()
    print('end!')
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Jalen Chu 微信支付

微信支付

Jalen Chu 支付宝

支付宝

Jalen Chu 公众号

公众号