确保 Pandas 数据框的列具有唯一值

时间:2021-06-19 15:46:49

标签: python pandas algorithm dataframe

鉴于以下内容:

information_dict_from = {
    "v1": {0: "type a", 1: "type b"},
    "v2": {0: "type a", 1: "type b", 3: "type c"},
    "v3": {0: "type a", 1: "type b"},
}

data_from = pd.DataFrame(
    {
        "v1": [0, 0, 1, 1],
        "v2": [0, 1, 1, 3],
        "v3": [0, 1, 1, 0],
    }
)

我想将其转换为:


information_dict_to = {
    "v1": {0: "type a", 1: "type b"},
    "v2": {2: "type a", 3: "type b", 4: "type c"},
    "v3": {5: "type a", 6: "type b"},
}

data_to = pd.DataFrame(
    {
        "v1": [0, 0, 1, 1],
        "v2": [2, 3, 3, 4],
        "v3": [5, 6, 6, 5],
    }
)

注意 - 在转换数据帧中的值后,列是互斥的 (set(df['v1']) - set(df['v2']) == set(df['v1'])) ,并且 information_dict_from[<var>] 键到相应 <var> 列之间的映射被保留。

1 个答案:

答案 0 :(得分:0)

# copy *_to from *_from
data_to = data_from.copy()
information_dict_to = information_dict_from.copy()

# set the unique increase counter
val = 0
for col in data_from: # for each column (v1, v2, v3)
    u_val_map = {} # create the mapping dict
    for u in data_from[col].unique(): # get all posible value
        data_to.loc[data_from[col]==u, col] = val #set new unique val
        u_val_map[u] = val # record mapping dict
        val+=1 # increase 1 to make new val
    # updating dict for the key==col by using mapping dict
    information_dict_to.update({col:{
        u_val_map[key]:information_dict_from[col][key]
        for key in information_dict_from[col]}})

然后

>>>data_to
    v1  v2  v3
0   0   2   5
1   0   3   6
2   1   3   6
3   1   4   5
>>>information_dict_to
{'v1': {0: 'type a', 1: 'type b'},
 'v2': {2: 'type a', 3: 'type b', 4: 'type c'},
 'v3': {5: 'type a', 6: 'type b'}}
相关问题