简体   繁体   中英

Ensure columns of a pandas dataframe have unique values

Given the following:

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],
    }
)

I'd like to transform it to:


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],
    }
)

Note - after transforming the values in the dataframe columns are exclusive ( set(df['v1']) - set(df['v2']) == set(df['v1']) ) , and the mapping between information_dict_from[<var>] keys to the corresponding <var> column is preserved.

# 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]}})

then

>>>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'}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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