Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas replace dataframe cell values

Tags:

python

pandas

I have a dataframe where some cells have a string like "<0.5".

I would like to iterate over the entire dataframe, and for any cells that contain the less than sign, I would like to replace the entire cell with 0.0.

So for example, <0.4 becomes 0.0

Edit to add some code:

df = pd.read_csv(infl)
for i in range(df.shape[0]):
    for j in range(df.shape[1]):
        if "<" in df.ix[i,j]:
            df.ix[i,j] = 0.0

This produces the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\00Working\81_WinPython_32bit_2.7.5.3\python-2.7.5\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile
    execfile(filename, namespace)
  File "Z:/working/MINING/2015/01_read_data.py", line 24, in <module>
    if "<" in df.ix[i,j]:
TypeError: argument of type 'numpy.int64' is not iterable

This code also does not work:

df = pd.read_csv(infl)
for i in range(df.shape[0]):
    for j in range(df.shape[1]):
        if '<' in df.iloc[i][j]:
            df[i,j] = 0.0

This code gives the same error as above.

like image 950
Flux Capacitor Avatar asked Sep 02 '25 14:09

Flux Capacitor


1 Answers

You could use applymap() function to do particular item in all cells,

In [92]: df
Out[92]: 
     a    b
0    1  <.3
1    2    2
2  <.3  <.4
3    4    5

In [93]: df.applymap(lambda x: 0 if "<" in str(x) else x)
Out[93]: 

   a  b
0  1  0
1  2  2
2  0  0
3  4  5

converting cell lambda x to string since int/float will fail for in.

like image 161
WoodChopper Avatar answered Sep 05 '25 10:09

WoodChopper