如何用条件替换Pandas数据框中所有列中的所有值
问题描述:
我有以下数据框:
In [11]: import pandas as pd
In [12]: mydict = {'foo':[0, 0.3], 'bar':[1,0.55], 'qux': [0.3,4.1]}
In [13]: df = pd.DataFrame.from_dict(mydict, orient='index')
In [14]: df
Out[14]:
0 1
qux 0.3 4.10
foo 0.0 0.30
bar 1.0 0.55
我要做的是将所有小于1的值替换为0. 屈服:
What I want to do is to replace all values that is less than 1 with 0. Yielding:
0 1
qux 0 4.10
foo 0 0
bar 1.0 0
我该如何实现?
答
使用布尔索引并传递条件:
Use boolean indexing and pass the condition:
In [155]:
df[df<1] = 0
df
Out[155]:
0 1
bar 1 0.0
foo 0 0.0
qux 0 4.1
仅显示在这里执行df < 1
会发生什么,将返回一个布尔索引:
Just to show what is happening here performing df < 1
will return a boolean index:
In [156]:
df < 1
Out[156]:
0 1
bar False True
foo True True
qux True False
然后我们将其作为掩码传递给df
,然后可以将新值指定为df[df<1]
,请参见
This we then pass to df
as a mask and can then assign the new values as df[df<1]
see the docs for further examples