在数据框的一列上应用 unique / distinct 后获取其他列 pandas

Get other columns after applying unique / distinct on one column of dataframe pandas

问题

我正在尝试获取数据框中的选定列,但是我尝试获取选定列的列必须是唯一的

场景

所以,Dataframe 的所有值都是字符串,请不要尝试关联任何查看数据的内容。 DF 看起来像这样:

A B C D E
12 Hello 1 txt num
123 Bello 2 txt doub
7 nice 1 txt num
54 duke 1 txt num
9901 - 3 char doub
63.38 - 4 char deci
8331 - 3 char doub
91 , 5 char num

我想在 C 列上 运行 一个 .unique() 并获得 DE 列以及 C.

审判

现在,我已经实现了我想要的输出,但我相信这也可以用很少的几行来完成。作为记录,这是我的代码。 main_df 包含上面的 table.

dependent_variables = ["D", "E"]
Dictionary = pd.DataFrame()

new_book = {}
dependent_variables_index = []

for no, col in enumerate(main_df.columns):
    print(no, col)
    if col in dependent_variables:
        dependent_variables_index.append(no)

for cid in total_categories:
    try:
        new_book[cid] = main_df[main_df["C"] == int(cid)].iloc[0, dependent_variables_index].to_dict()
    except KeyError:
        new_book[cid] = main_df[main_df["C"] == str(cid)].iloc[0, dependent_variables_index].to_dict()

for k, v in new_book.items():
    Dictionary = Dictionary.append(v, ignore_index=True)

Dictionary.index = list(new_book.keys())
Category_Dictionary = Dictionary.reset_index().rename(columns={"index": "C"})

预期输出

C D E
1 txt num
2 txt doub
3 char doub
4 char deci
5 char num

同样,我可以生成此输出,但是我正在寻找更优化的方法来执行相同的操作。

是吗:

df[['C','D','E']].drop_duplicates('C')

输出:

   C     D     E
0  1   txt   num
1  2   txt  doub
4  3  char  doub
5  4  char  deci
7  5  char   num