你能告诉我什么时候使用这些矢量化方法和基本的例子吗?
我看到map是一个系列方法,而其余的是DataFrame方法。我对apply和applymap方法感到困惑。为什么我们有两个方法来应用一个函数到一个数据帧?再一次,简单的例子说明用法将是伟大的!
你能告诉我什么时候使用这些矢量化方法和基本的例子吗?
我看到map是一个系列方法,而其余的是DataFrame方法。我对apply和applymap方法感到困惑。为什么我们有两个方法来应用一个函数到一个数据帧?再一次,简单的例子说明用法将是伟大的!
当前回答
基于cs95的答案
map在Series ONLY上定义 applymap只在DataFrames上定义 apply定义在BOTH上
举几个例子
In [3]: frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [4]: frame
Out[4]:
b d e
Utah 0.129885 -0.475957 -0.207679
Ohio -2.978331 -1.015918 0.784675
Texas -0.256689 -0.226366 2.262588
Oregon 2.605526 1.139105 -0.927518
In [5]: myformat=lambda x: f'{x:.2f}'
In [6]: frame.d.map(myformat)
Out[6]:
Utah -0.48
Ohio -1.02
Texas -0.23
Oregon 1.14
Name: d, dtype: object
In [7]: frame.d.apply(myformat)
Out[7]:
Utah -0.48
Ohio -1.02
Texas -0.23
Oregon 1.14
Name: d, dtype: object
In [8]: frame.applymap(myformat)
Out[8]:
b d e
Utah 0.13 -0.48 -0.21
Ohio -2.98 -1.02 0.78
Texas -0.26 -0.23 2.26
Oregon 2.61 1.14 -0.93
In [9]: frame.apply(lambda x: x.apply(myformat))
Out[9]:
b d e
Utah 0.13 -0.48 -0.21
Ohio -2.98 -1.02 0.78
Texas -0.26 -0.23 2.26
Oregon 2.61 1.14 -0.93
In [10]: myfunc=lambda x: x**2
In [11]: frame.applymap(myfunc)
Out[11]:
b d e
Utah 0.016870 0.226535 0.043131
Ohio 8.870453 1.032089 0.615714
Texas 0.065889 0.051242 5.119305
Oregon 6.788766 1.297560 0.860289
In [12]: frame.apply(myfunc)
Out[12]:
b d e
Utah 0.016870 0.226535 0.043131
Ohio 8.870453 1.032089 0.615714
Texas 0.065889 0.051242 5.119305
Oregon 6.788766 1.297560 0.860289
其他回答
快速的总结
DataFrame。Apply一次操作整个行或列。 DataFrame。applymap系列。apply和Series。Map操作1 元素。
系列。应用和系列。地图是相似的,经常可以互换。下面osa的回答中讨论了他们之间的一些细微差异。
apply和applymap之间的区别可能是最简单的解释:
Apply将整个列作为参数,然后将结果分配给该列
Applymap将单独的单元格值作为参数,并将结果分配回该单元格。
注意:如果apply返回单个值,你将得到这个值而不是赋值后的列,最终将只有一行而不是矩阵。
基于cs95的答案
map在Series ONLY上定义 applymap只在DataFrames上定义 apply定义在BOTH上
举几个例子
In [3]: frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])
In [4]: frame
Out[4]:
b d e
Utah 0.129885 -0.475957 -0.207679
Ohio -2.978331 -1.015918 0.784675
Texas -0.256689 -0.226366 2.262588
Oregon 2.605526 1.139105 -0.927518
In [5]: myformat=lambda x: f'{x:.2f}'
In [6]: frame.d.map(myformat)
Out[6]:
Utah -0.48
Ohio -1.02
Texas -0.23
Oregon 1.14
Name: d, dtype: object
In [7]: frame.d.apply(myformat)
Out[7]:
Utah -0.48
Ohio -1.02
Texas -0.23
Oregon 1.14
Name: d, dtype: object
In [8]: frame.applymap(myformat)
Out[8]:
b d e
Utah 0.13 -0.48 -0.21
Ohio -2.98 -1.02 0.78
Texas -0.26 -0.23 2.26
Oregon 2.61 1.14 -0.93
In [9]: frame.apply(lambda x: x.apply(myformat))
Out[9]:
b d e
Utah 0.13 -0.48 -0.21
Ohio -2.98 -1.02 0.78
Texas -0.26 -0.23 2.26
Oregon 2.61 1.14 -0.93
In [10]: myfunc=lambda x: x**2
In [11]: frame.applymap(myfunc)
Out[11]:
b d e
Utah 0.016870 0.226535 0.043131
Ohio 8.870453 1.032089 0.615714
Texas 0.065889 0.051242 5.119305
Oregon 6.788766 1.297560 0.860289
In [12]: frame.apply(myfunc)
Out[12]:
b d e
Utah 0.016870 0.226535 0.043131
Ohio 8.870453 1.032089 0.615714
Texas 0.065889 0.051242 5.119305
Oregon 6.788766 1.297560 0.860289
我只是想指出一点,我在这方面有过一些挣扎
def f(x):
if x < 0:
x = 0
elif x > 100000:
x = 100000
return x
df.applymap(f)
df.describe()
这不会修改数据帧本身,必须重新分配:
df = df.applymap(f)
df.describe()
除了其他答案,在一个系列中还有map和apply。
Apply可以从一个系列中生成一个DataFrame;然而,map只会在另一个系列的每个单元格中放入一个系列,这可能不是您想要的。
In [40]: p=pd.Series([1,2,3])
In [41]: p
Out[31]:
0 1
1 2
2 3
dtype: int64
In [42]: p.apply(lambda x: pd.Series([x, x]))
Out[42]:
0 1
0 1 1
1 2 2
2 3 3
In [43]: p.map(lambda x: pd.Series([x, x]))
Out[43]:
0 0 1
1 1
dtype: int64
1 0 2
1 2
dtype: int64
2 0 3
1 3
dtype: int64
dtype: object
另外,如果我有一个带有副作用的函数,比如“连接到web服务器”,为了清晰起见,我可能会使用apply。
series.apply(download_file_for_every_element)
Map不仅可以使用函数,还可以使用字典或其他系列。假设你想操纵排列。
Take
1 2 3 4 5
2 1 4 5 3
这个排列的平方是
1 2 3 4 5
1 2 5 3 4
你可以用map来计算。不确定self-application是否有文档记录,但它在0.15.1中可以工作。
In [39]: p=pd.Series([1,0,3,4,2])
In [40]: p.map(p)
Out[40]:
0 0
1 1
2 4
3 2
4 3
dtype: int64