如何对两个长格式的分类数据熊猫列进行分组?

时间:2019-03-15 01:04:48

标签: python pandas categorical-data

永远尝试。似乎应该更简单。如何获取具有重复值的长格式df,并将所有重复值组合在一起?我尝试了groupby,但这不是数字数据。试图枢轴转动,但这也不起作用。

     import pandas as pd
        input = pd.DataFrame( {
            "CombinedID" : ["Alice1084", "Alice1084", "Alice1084", "Alice1084", "Alice1084",
                            "Mallory1085", "Mallory1085", "Mallory1085",
                            "Bob42", "Bob42", "Bob42", "Bob42"],
            "Occupation" : ["Comedian", "Comedian", "Comedian", "Comedian", "Comedian",
                            "Surgeon", "Surgeon", "Surgeon",
                            "Philosopher", "Philosopher", "Philosopher", "Philosopher"] } )

        output = pd.DataFrame({"CombinedID" : ["Alice1084","Mallory1085","Bob42"],
                               "Occupation" : ["Comedian", "Surgeon", "Philosopher"]})
    print(output)
    CombinedID   Occupation
0    Alice1084     Comedian
1  Mallory1085      Surgeon
2        Bob42  Philosopher

0 个答案:

没有答案