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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With