Home > front end >  How to replace the values of nested lists stored in python dataframe?
How to replace the values of nested lists stored in python dataframe?

Time:11-09

I have the following dataframe

data = [[[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]], [[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]]]

df = pd.DataFrame(data)

Also, I have a second dataframe df2:

data2 = [[1, 10], [1, 15], [1, 14], [1, 20], [1, 18]]
df2 = pd.DataFrame(data2, columns=['dir', 'line'])

The values of the nested lists of df represent the Index of df2. I would like to replace the values of the nested lists of df with the values of the column line of df2. The expected output is the following:

finalData = [[[[[15, 14, 10], [15]],[[15, 14], [20]]],
        [[[[15, 14], [20]]]], [[[15]]]], [[[[15, 14, 10], [15]],[[15, 14], [20]]],
        [[[[15, 14], [29]]]], [[[15]]]]]

finaldf = pd.DataFrame(finalData)

CodePudding user response:

Try this recursive solution

data = [[[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]], [[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]]]
data2 = [[1, 10], [1, 15], [1, 14], [1, 20], [1, 18]]
def new_data(data, data2):
  res = []
  for i in data:
    if isinstance(i, list):
      res.append(new_data(i, data2))
    else:
      res.append(data2[i][1])
  return res
final_data = new_data(data, data2)
print(final_data)

# output: 
[[[[[15, 14, 10], [15]], [[15, 14], [18]]], [[[[15, 14], [18]]]], [[[15]]]],
     [[[[15, 14, 10], [15]], [[15, 14], [18]]], [[[[15, 14], [18]]]], [[[15]]]]]

CodePudding user response:

Here you are.

data = [[[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]], [[[[1, 2, 0], [1]],[[1, 2], [4]]],
        [[[[1, 2], [4]]]], [[[1]]]]]
df = pd.DataFrame(data)
data2 = [[1, 10], [1, 15], [1, 14], [1, 20], [1, 18]]
df2 = pd.DataFrame(data2, columns=['dir', 'line'])

def fn(index):
    if isinstance(index, list):
        return [fn(i) for i in index]
    else:
        return df2['line'][index]

final = df.applymap(fn)
  • Related