简体   繁体   English

Python Pandas数据框转换

[英]Python pandas data frame transformation

I'm trying to transform a panda data frame , its original structure is 我正在尝试转换熊猫数据框,其原始结构为

USER_ID , Buisiness_ID, Rating
abcde   , a1b1c2      , 3
abcde   , a2b3c2      , 3
afgh    , a1b3        ,1 

and I'm trying to transform it into 我正在尝试将其转化为

        a1b1c2       a2b3c2      a1b3
abcde     3            3
afgh                               1

what would be the best way to do this? 最好的方法是什么?

You are looking for the pivot operation, which looks like reshaping the table from long to wide: 您正在寻找数据透视操作,它看起来像是将表从长整形改成宽整形:

import pandas as pd
print df.pivot(index='USER_ID', columns='Buisiness_ID')

Output: 输出:

             Rating            
Buisiness_ID a1b1c2 a1b3 a2b3c2
USER_ID                        
abcde             3  NaN      3
afgh            NaN    1    NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM