Pandas DataFrame ApplyMap 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21726275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:41:33  来源:igfitidea点击:

Pandas DataFrame ApplyMap method

pythonpandas

提问by Baktaawar

I wanted to try out the functionality of applymapmethod of Pandas DataFrameobject. Here is the Use case:

我想尝试applymapPandasDataFrame对象的方法的功能。这是用例:

Let say my DataFrame df1is as follows:

假设我的 DataFramedf1如下:

Age   ID       Name
0   27  101    John
1   22  102    Bob
2   19  103    Alok
3   27  104    Tom
4   32  105    Matt
5   19  106    Steve
6    5  107    Tom
7   55  108    Dick
8   67  109    Harry

Now I want to create a flag variable with the logic that if length of element is less than 2, then flag=1 else flag=0.

现在我想创建一个标志变量,其逻辑是如果元素的长度小于 2,则标志 = 1 否则标志 = 0。

In order to run this element-wise, I wanted to use applymapmethod. So for that I created a user defined function as follows:

为了逐个运行这个元素,我想使用applymap方法。为此,我创建了一个用户定义的函数,如下所示:

def f(x): 
   if len(str(x))>2: 
       df1['Flag']=1
   else: 
      df1['Flag']=0

Then I ran df1.applymap(f)which gave:

然后我跑了df1.applymap(f)它给:

    Age    ID  Name
0  None  None  None
1  None  None  None
2  None  None  None
3  None  None  None
4  None  None  None
5  None  None  None
6  None  None  None
7  None  None  None
8  None  None  None

instead of creating a flag variable with the flag value. How can I achieve the desired functionality using applymap?

而不是使用标志值创建标志变量。如何使用 实现所需的功能applymap

Can't we use the DataFrame variable name or pandas statement inside the user defined function? I.e., is df1['Flag']valid inside the definition of f()?

我们不能在用户定义的函数中使用 DataFrame 变量名或 pandas 语句吗?即,df1['Flag']f()?的定义内是否有效?

回答by Bonlenfum

the function f(x)is not special to pandas -- it is just a regular python function. So the only data in scope within fis the variable xOther members of df1are not available.

该函数f(x)对于pandas 并不特殊——它只是一个普通的python 函数。因此,范围内的唯一数据f是变量xOther 的成员df1不可用。

From applymapdocs:

来自applymap文档:

func : function

Python function, returns a single value from a single value

功能:功能

Python 函数,从单个值返回单个值

So you could try this:

所以你可以试试这个:

def f(x):
    if len(str(x)) <= 3: return 1
    else: return 0

Outputting 1/0 for each element in the frame when applied:

应用时为框架中的每个元素输出 1/0:

df1.applymap(f)

>>>
   Age  ID  Name
0    1   1     0
1    1   1     1
2    1   1     0
3    1   1     1
4    1   1     0
5    1   1     0
6    1   1     1
7    1   1     0
8    1   1     0

To use the result to add another variable in each row, you need one value per row , e.g.,

要使用结果在每一行中添加另一个变量,您需要每行一个值,例如,

df1['Flag'] = df1.applymap(f).all(axis=1).astype(bool)

>>> df1

   Age   ID   Name   Flag
0   27  101   John  False
1   22  102    Bob   True
2   19  103   Alok  False
3   27  104    Tom   True
4   32  105   Matt  False
5   19  106  Steve  False
6    5  107    Tom   True
7   55  108   Dick  False
8   67  109  Harry  False

Also check out https://stackoverflow.com/a/19798528/1643946which covers apply, mapas well as applymap.

还检查了https://stackoverflow.com/a/19798528/1643946覆盖applymap以及applymap