1

Could you let me know how to onvert decimal to binary value of rows in multindex dataframe?

below is dataframe I used

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"])
print (df)

below is printed dataframe

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

I want to convert decimal to binary values of rows(func1 - In - Name1, Name2) in multi-index dataframe.

below is expected df I want.

                            Val1    Val2    Val3    Val4    Val5
 Function   Type    Name                    
 env        In      Volt    Max     Typ     Min     Max     Max
                    Temp    High    Mid     Low     High    Low
                    BD#     1       2       3       4       5
 func1      In      Name1   11      100     11      11      11
                    Name2   101     111     110     1001    100
            out     Name3   6       6       3       4       5

I have tried to get right results but I failed. TT

Plz let me know how to resolve it simply.

2 Answers 2

1

Use MultiIndex.get_level_values for create conditions, chain together and set new values by f-strings:

m1 = df.index.get_level_values(0) == 'func1'
m2 = df.index.get_level_values(1) == 'In'

df[m1 & m2] = df[m1 & m2].astype(int).applymap(lambda x: f'{x:b}')
print (df)
                     Val1 Val2 Val3  Val4 Val5
Function Type Name                            
env      In   Volt    Max  Typ  Min   Max  Max
              Temp   High  Mid  Low  High  Low
              BD#       1    2    3     4    5
func1    In   Name1    11  100   11    11   11
              Name2   101  111  110  1001  100
         out  Name3     6    6    3     4    5
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I really appreciate your answer. I felt that it is hard to change values in row of dataframe. I didn't even think of this solution.
1

By creating mask of the dataframe:

mask = ((df.index.get_level_values('Function') == 'func1')&
                (df.index.get_level_values('Type') == 'In')&
                (df.index.get_level_values('Name').isin(['Name1', 'Name2'])))

df[mask] = df[mask].astype(int).applymap(lambda x: format(x, 'b'))  

print(df[mask])

                     Val1 Val2 Val3  Val4 Val5
Function Type Name                            
env      In   Volt    Max  Typ  Min   Max  Max
              Temp   High  Mid  Low  High  Low
              BD#       1    2    3     4    5
func1    In   Name1    11  100   11    11   11
              Name2   101  111  110  1001  100
         out  Name3     6    6    3     4    5

1 Comment

I learned using mask of dataframe and applymap thanks to your answer. I appreciate it!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.