比较来自世界各地的卖家的域名和 IT 服务价格

Pandas DataFrame: 将函数应用于所有列

我可以用
.map/func/

对于任何列 df, 例如:


df=DataFrame/{'a':[1,2,3,4,5,6],'b':[2,3,4,5,6,7]}/

df['a']=df['a'].map/lambda x: x > 1/


我也可以:


df['a'],df['b']=df['a'].map/lambda x: x > 1/,df['b'].map/lambda x: x > 1/


是否有更多的pythonic方法来将函数应用于所有列或整个框架 /没有周期/?
已邀请:

君笑尘

赞同来自:

如果我正确理解你,你正在寻找一种方法
applymap

.


>>> print df
A B C
0 -1 0 0
1 -4 3 -1
2 -1 0 2
3 0 3 2
4 1 -1 0
>>> print df.applymap/lambda x: x>1/
A B C
0 False False False
1 False True False
2 False False True
3 False True True
4 False False False

石油百科

赞同来自:

以。。。开始
0.20.0

然后,你可以使用
transform



In [578]: df.transform/lambda x: x > 1/
Out[578]:
A B C
0 False False False
1 False True False
2 False False True
3 False True True
4 False False False

In [579]: df
Out[579]:
A B C
0 -1 0 0
1 -4 3 -1
2 -1 0 2
3 0 3 2
4 1 -1 0


在这个简化的情况下,为什么不使用
df > 1

?


In [582]: df > 1
Out[582]:
A B C
0 False False False
1 False True False
2 False False True
3 False True True
4 False False False

要回复问题请先登录注册