I have this dataframe
0 312229 12 {"from":[3,4],"to":[7,4],"color":2},{"from":[3... ["v","b","c","c","a","h","i","e","r","s","f","... "#ff0000","#00fe00","#0000ff","#d2ea9a","#407f... "place","cartable","gomme","bureau","bibliothè...
4 400606 12 {"from":[3,4],"to":[7,4],"color":2},{"from":[3... ["v","b","c","c","a","h","i","e","r","s","f","... "#ff0000","#00fe00","#0000ff","#d2ea9a","#407f... "place","cartable","gomme","bureau","bibliothè...
8 410051 12 {"from":[3,4],"to":[7,4],"color":2},{"from":[3... ["v","b","c","c","a","h","i","e","r","s","f","... "#ff0000","#00fe00","#0000ff","#d2ea9a","#407f... "place","cartable","gomme","bureau","bibliothè...
...
And I need to convert this dataframe column
0 ["v","b","c","c","a","h","i","e","r","s","f","...
4 ["v","b","c","c","a","h","i","e","r","s","f","...
8 ["v","b","c","c","a","h","i","e","r","s","f","...
12 ["v","b","c","c","a","h","i","e","r","s","f","...
16 ["r","h","a","y","l","q","j","e","b","s"],["m"...
20 ["r","h","a","y","l","q","j","e","b","s"],["m"...
24 ["r","h","a","y","l","q","j","e","b","s"],["m"...
28 ["r","h","a","y","l","q","j","e","b","s"],["m"...
32 ["s","y","i","e","j","w","b","s","h","p"],["m"...
36 ["s","y","i","e","j","w","b","s","h","p"],["m"...
40 ["s","y","i","e","j","w","b","s","h","p"],["m"...
44 ["s","y","i","e","j","w","b","s","h","p"],["m"...
I want to transform it like this
{'0': {'0': 'v', '1': 'b', '2': 'c', '3': 'c', '4': 'a', '5': 'h', '6': 'i', '7': 'e', '8': 'r', '9': 's', '10': 'f', '11': 'j'}, '1': {'0': 'd', '1': 'i', '2': 'w', '3': 's', '4': 's', '5': 'r', '6': 'i', '7': 'f', '8': 'y', '9': 'y', '10': 'f', '11': 'c'}, '2': {'0': 'j', '1': 'b', '2': 'm', '3': 'w', '4': 'd', '5': 'q', '6': 's', '7': 'q', '8': 't', '9': 'w', '10': 'e', '11': 'm'}, '3': {'0': 'x', '1': 'l', '2': 'm', '3': 'm', '4': 'l', '5': 's', '6': 'o', '7': 'x', '8': 'd', '9': 'q', '10': 'u', '11': 't'}, '4': {'0': 'l', '1': 'i', '2': 'f', '3': 'p', '4': 'l', '5': 'a', '6': 'c', '7': 'e', '8': 't', '9': 'u', '10': 't', '11': 'o'}, '5': {'0': 'm', '1': 'o', '2': 's', '3': 'b', '4': 'r', '5': 't', '6': 'c', '7': 'y', '8': 'z', '9': 'v', '10': 'r', '11': 'r'}, '6': {'0': 'j', '1': 't', '2': 'x', '3': 'c', '4': 'a', '5': 'r', '6': 't', '7': 'a', '8': 'b', '9': 'l', '10': 'e', '11': 'o'}, '7': {'0': 'b', '1': 'h', '2': 'k', '3': 'm', '4': 'd', '5': 'b', '6': 'r', '7': 'y', '8': 'q', '9': 'u', '10': 'i', '11': 'y'}, '8': {'0': 'y', '1': 'è', '2': 's', '3': 'r', '4': 'h', '5': 'g', '6': 'o', '7': 'm', '8': 'm', '9': 'e', '10': 'w', '11': 'h'}, '9': {'0': 'u', '1': 'q', '2': 'p', '3': 'c', '4': 's', '5': 'c', '6': 'x', '7': 'b', '8': 'k', '9': 'e', '10': 'd', '11': 'o'}, '10': {'0': 'u', '1': 'u', '2': 'o', '3': 'l', '4': 'q', '5': 'v', '6': 'y', '7': 'y', '8': 'b', '9': 'y', '10': 'e', '11': 'h'}, '11': {'0': 'r', '1': 'e', '2': 'o', '3': 'u', '4': 'j', '5': 'b', '6': 'u', '7': 'r', '8': 'e', '9': 'a', '10': 'u', '11': 'k'}}
That is, I need to enumerate the data which are located in this column in this form
I wrote the code, but it doesn't display exactly what I want to see.
def js(value):
val = {str(j):{str(i):letter for i,letter in enumerate(info)} for j,info in enumerate(value)}
return val
...
total['data_matrix'] = total['data_matrix'].apply(js)
This code outputs data in a way that is not entirely correct
{'0': {'0': '['}, '1': {'0': '"'}, '2': {'0': 'v'}, '3': {'0': '"'}, '4': {'0': ','}, '5': {'0': '"'}, '6': {'0': 'b'}, '7': {'0': '"'}, '8': {'0': ','}, '9': {'0': '"'}, '10': {'0': 'c'}, '11': {'0': '"'}, '12': {'0': ','}, '13': {'0': '"'}, '14': {'0': 'c'}, '15': {'0': '"'}, '16': {'0': ','},...
The data that is in the first row of this column looks like this:
["v","b","c","c","a","h","i","e","r","s","f","j"],["d","i","w","s","s","r","i","f","y","y","f","c"],["j","b","m","w","d","q","s","q","t","w","e","m"],["x","l","m","m","l","s","o","x","d","q","u","t"],["l","i","f","p","l","a","c","e","t","u","t","o"],["m","o","s","b","r","t","c","y","z","v","r","r"],["j","t","x","c","a","r","t","a","b","l","e","o"],["b","h","k","m","d","b","r","y","q","u","i","y"],["y","è","s","r","h","g","o","m","m","e","w","h"],["u","q","p","c","s","c","x","b","k","e","d","o"],["u","u","o","l","q","v","y","y","b","y","e","h"],["r","e","o","u","j","b","u","r","e","a","u","k"]
I could use replace and then everything would be displayed well, but I need to convert the last two columns in the same way.For example:
{'0': '#ff0000', '1': '#00fe00', '2': '#00fe00', '3': '#d2ea9a', ....
Please tell me how can I do this
CodePudding user response:
I guess this will help.
def js_Conv(value):
ll = value.split(',')
js = {str(i):letter for i,letter in enumerate(ll)}
return js
df['data_matrix'] = df['data_matrix'].apply(js_Conv)