0

I am trying to set or floor values which go beyond a defined threshold. I am using the .ix[] method for it. However, I am unable to get it done using the below is the code (python 2).

Please teach me why this is wrong & any other faster methods.

df = DataFrame({"v1" : np.random.randn(10), "v2" : np.random.randn(10), "v3" : [np.nan]*10})

print "pre floor"
print df 

f_dict = {"v1": 0, "v2": 0}
#flooring value treatment
for key, val in f_dict.iteritems():
    print df.ix[df[key]<val,key] = val

print df 

#ERROR:
    print df.ix[df[key]<val,key]=0
                                ^
SyntaxError: invalid syntax
petarap
  • 257
  • 1
  • 4
  • 13

2 Answers2

2

So here is what is happening with your solution

  print df.ix[df[key]<val,key] = val #syntax error

Here you are trying to print and also assign it to variable val but that's not supportable.

The code below serves as e example for you.

    df.loc[df[key]<val,key] = val
    print(val)
Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34
1

try this:

for key, val in f_dict.iteritems():
    df.loc[df[key]<val, key] = val
print df

See here for a related answer

Sam
  • 4,000
  • 20
  • 27