简体   繁体   English

Pandas DataFrame ApplyMap方法

[英]Pandas DataFrame ApplyMap method

I wanted to try out the functionality of applymap method of Pandas DataFrame object. 我想试试Pandas DataFrame对象的applymap方法的功能。 Here is the Use case: 这是用例:

Let say my DataFrame df1 is as follows: 假设我的DataFrame df1如下:

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,则flag = 1 else flag = 0。

In order to run this element-wise, I wanted to use applymap method. 为了运行这个元素,我想使用applymap方法。 So for that I created a user defined function as follows: 为此,我创建了一个用户定义的函数,如下所示:

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? 我们不能在用户定义的函数中使用DataFrame变量名或pandas语句吗? Ie, is df1['Flag'] valid inside the definition of f() ? 即, df1['Flag']f()的定义内有效吗?

the function f(x) is not special to pandas -- it is just a regular python function. 函数f(x)对于pandas并不特殊 - 它只是一个常规的python函数。 So the only data in scope within f is the variable x Other members of df1 are not available. 因此f中范围内唯一的数据是变量x df1其他成员不可用。

From applymap docs: 来自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 , eg, 要使用结果在每行中添加另一个变量,每行需要一个值,例如,

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/1643946 which covers apply , map as well as applymap . 另请applymap https://stackoverflow.com/a/19798528/1643946 ,其中包括applymap以及applymap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM