我有一个数据框,其中某些单元格具有类似“ <0.5”的字符串。
我想遍历整个数据帧,对于包含小于号的任何单元格,我想用0.0替换整个单元格。
因此,例如<0.4变为0.0
编辑以添加一些代码:
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
Run Code Online (Sandbox Code Playgroud)
产生错误:
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
Run Code Online (Sandbox Code Playgroud)
此代码也不起作用:
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
Run Code Online (Sandbox Code Playgroud)
此代码给出与上述相同的错误。
您可以使用applymap()函数在所有单元格中执行特定项目,
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
Run Code Online (Sandbox Code Playgroud)
将单元格转换lambda x为字符串,因为int / float将失败in。
| 归档时间: |
|
| 查看次数: |
3197 次 |
| 最近记录: |