I want to paste the list value into a specific part of the multi index dataframe. (Python)

Asked 2 years ago, Updated 2 years ago, 88 views

I want to paste the list value into a specific part of the multi index dataframe.

The code below is the data frame that I am currently practicing.

I want to put list1 and list2 in the specific column area of the data frame I want.

list1 -> df[func1, In][Val6]

list2 -> df[func2, In][Val6]

from pandas import Series, DataFrame
raw_data = {'Function': ['env', 'env', 'env', 'func1', 'func1', 'func1'],
            'Type': ['In', 'In', 'In', 'In','In', 'out'],
            'Name': ['Volt', 'Temp', 'BD#', 'Name1','Name2', 'Name3'],
            'Val1': ['Max', 'High', '1', '3', '5', '6'],
            'Val2': ['Typ', 'Mid', '2', '4', '7', '6'],
            'Val3': ['Min', 'Low', '3', '3', '6', '3'],
            'Val4': ['Max', 'High', '4', '3', '9', '4'],
            'Val5': ['Max', 'Low', '5', '3', '4', '5'] }
df = DataFrame(raw_data)
df= df.set_index(["Function", "Type","Name"])
df['Val6'] = np.NaN

list1 = [1,2]
list2 = [3,4]

print (df)

Below is a printed data frame.

                     Val1 Val2 Val3  Val4 Val5  Val6
Function Type Name                                  
env      In   Volt    Max  Typ  Min   Max  Max   NaN
              Temp   High  Mid  Low  High  Low   NaN
              BD#       1    2    3     4    5   NaN
func1    In   Name1     4    2    3     4    5   NaN
              Name2     6    7    6     9    4   NaN
         out  Name3     6    6    3     4    5   NaN
              Name4     3    3    4     5    6   NaN

Below is the result I want. I would like to replace NaN of the data frame with list1 and list2 sequentially.

                     Val1 Val2 Val3  Val4 Val5  Val6
Function Type Name                                  
env      In   Volt    Max  Typ  Min   Max  Max   NaN
              Temp   High  Mid  Low  High  Low   NaN
              BD#       1    2    3     4    5   NaN
func1    In   Name1     4    2    3     4    5     1
              Name2     6    7    6     9    4     2
         out  Name3     6    6    3     4    5     3
              Name4     3    3    4     5    6     4

Attempted using Concat or replace function, but failed. In order to apply it to a more complex data frame, I wanted to get an answer by using multi-index masking, but I can't think of a good idea.

I tried the following attempts, but an error occurred. I ask for your help.ㅠ<

list1=[1,2]
list2=[3,4]
m1 = df.index.get_level_values(0) == 'func1'
m2 = df.index.get_level_values(1) == 'In'

list1 = [float(i) for i in list1]
df_list1=pd.DataFrame(list1)

df.replace(df[m1&m2]['Val6'], df_list1)

python dataframe pandas list

2022-09-22 19:46

1 Answers

df["Val6"]["func1"]["In"] = list1
df["Val6"]["func1"]["out"] = list2

print(df)
                     Val1 Val2 Val3  Val4 Val5  Val6
Function Type Name                                  
env      In   Volt    Max  Typ  Min   Max  Max   NaN
              Temp   High  Mid  Low  High  Low   NaN
              BD#       1    2    3     4    5   NaN
func1    In   Name1     3    4    3     3    3   1.0
              Name2     5    7    6     9    4   2.0
         out  Name3     6    6    3     4    5   3.0
              Name4     3    3    4     5    6   4.0


2022-09-22 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.