There are so many questions about replacing some rows or columns or particular values, but I haven't found what I am looking for. Imagine a dataframe like this,
a b c d
a 0.354511 0.416929 0.704512 0.598345
b 0.948605 0.473364 0.154856 0.637639
c 0.250829 0.130928 0.682998 0.056049
d 0.504516 0.880731 0.216192 0.314724
And now I would like to replace all values based on a condition with something else (no matter in which column or row they are). Let's say I want to replace all values < 0.5 with np.nan.
I have tried several things and nothing worked (i.e. nothing happened, the dataframe remained unchanged).
Example code here:
frame = pd.DataFrame(np.random.rand(4,4),index=['a','b','c','d'], columns=['a','b','c','d'])
print frame
for row,col in enumerate(frame):
frame.replace(frame.ix[row,col]<0.5,np.nan,inplace=True)
print frame
or
for row,col in enumerate(frame):
if frame.ix[row,col]<=0.5:
M.ix[row,col]=np.nan
print M
but in the end,
a b c d
a 0.600701 0.823570 0.159012 0.615898
b 0.234855 0.086080 0.950064 0.982248
c 0.440625 0.960078 0.191975 0.598865
d 0.127866 0.537867 0.434326 0.507635
a b c d
a 0.600701 0.823570 0.159012 0.615898
b 0.234855 0.086080 0.950064 0.982248
c 0.440625 0.960078 0.191975 0.598865
d 0.127866 0.537867 0.434326 0.507635
- they are identical, no NaNs instead of small values. Where is the problem?
