简体   繁体   中英

modifying a multi column dataframe

I create a multi column (nested columns) that looks like this:

  input          action result
      1  2  3  4 action      1 2 3 4
0    89  3  0  5

Then I want to add values to it so it looks like this:

  input          action result
      1  2  3  4 action      1   2  3   4
0    89  3  0  5     64      1  54  0  34

here's how I make that dataframe in the first place (this works):

def create_memory_from_input(input: dict) -> pd.DataFrame:
    ''' creates a dataframe from input dictionary'''
    arrays = [
        ['input' for k in sorted(input.keys())] + ['action'] + ['result' for k in sorted(input.keys())],
        [k for k in sorted(input.keys())] + ['action'] + [k for k in sorted(input.keys())]]
    tuples = list(zip(*arrays))
    index = pd.MultiIndex.from_tuples(tuples)
    values = [[v for _,v in sorted(input.items())] + [''] + ['' for _,v in sorted(input.items())]]
    return pd.DataFrame(list(values), columns=index)

Here's the code I have to append the action and result to the dataframe but its not working. Am I referencing the nested columns correctly?

input = {2:3, 1:89, 4:5, 3:0}
original = create_memory_from_input(input)
action = 64
result = {2:54, 1:1, 4:34, 3:0}
original['action']['action'][
    (original['input'][1] == 89) &
    (original['input'][2] == 3) &
    (original['input'][3] == 0) &
    (original['input'][4] == 5)] = action

Any feedback is appreciated. I thought about making a new dataframe and then merging on the input columns but that doesn't seem as efficient as simply filtering the dataframe and setting the columns to the correct values.

what am I doing wrong?

You should use loc in this situation otherwise you get a chained assignment. See this article for further clarity.

The code using loc to append the data to your DataFrame looks like this:

input = {2:3, 1:89, 4:5, 3:0}
original = create_memory_from_input(input)
action = 64
result = {2:54, 1:1, 4:34, 3:0}

original.loc[0, ('action', 'action')] = action
for num in range(1, 5):
    original.loc[0, ('result', num)] = result[num]

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