Search code examples
pythonpandasdataframesubtraction

i'm trying to subtract these two dataframes but NaNs apear instead of values


the image of my code I made all of the dataframes needed and all the values but I don't know why NaNs appear.

MY CODE:

`import pandas as pd 

red_data = pd.DataFrame([[255,0,20],[210,30,0],[220,40,5],[240,10,30], 
[225,60,20]])
green_data = pd.DataFrame([[0,255,0],[10,240,5],[0,240,20],[5,220,30], 
[30,230,10]])
blue_data = pd.DataFrame([[30,10,220],[10,10,255],[0,0,240],[25,15,210], 
[0,20,220]])

red_data.columns =[["R","G","B"]]
green_data.columns =[["R","G","B"]]
blue_data.columns =[["R","G","B"]]

print(red_data)
print(green_data)
print(blue_data)


Feature1 = red_data.loc[:,"R"]-red_data.loc[:,"G"]
Feature2 = 2*red_data.loc[:,"B"] - red_data.loc [:,"R"] - red_data.loc 
[:,"G"]

print(Feature1)
print(Feature2)`

Solution

  • replace the relevant lines of your code with

    red_data.columns =["R","G","B"]
    green_data.columns =["R","G","B"]
    blue_data.columns =["R","G","B"]
    

    I think you created multiindex columns, possibly inadvertently