[英]How to apply function to a dataframe passing a column as an argument
I have a dataframe我有一个 dataframe
df:
| A | B |
1 | "USA" | 2 |
2 | "USA" |NAN|
3 | "GER" | 3 |
4 | "FRA" | 4 |
and a function which checks wether a value is in a certain bitmap if so returns true else returns false和一个 function 检查一个值是否在某个 bitmap 如果是则返回 true 否则返回 false
import pandas as pd
import numpy as np
import os
def valInBitmap(reason, bitmap):
if(pd.isna(bitmap)):
return(False)
if(reason == bitmap):
return(True)
n = 0
while(bitmap>=0):
if(bitmap<2**n):#4 < 2^3 <8 n= 3
#print("bitmap:" +str(bitmap) +" < 2^n: 2^" +str(n)+" = "+str(n**2))
if(reason == 2**(n-1)):#2 == 2^(3-1) = 4
return(True)
break
bitmap = bitmap - 2**(n-1)
n = 0
n =n+1
return(False)
Now I want to use the function on column "B" and return the outcome of each row to a new column "C"现在我想在“B”列上使用 function 并将每行的结果返回到新列“C”
df['C'] = df.apply(lambda row : valInBitmap(2,df['B']), axis = 1)
The final dataframe should look like this:最终的 dataframe 应如下所示:
df:
| A | B | C |
1 | "USA" | 2 | True |
2 | "USA" | NA | False |
3 | "GER" | 3 | True |
4 | "FRA" | 4 | False |
However When executing the code I get the following errror message但是,当执行代码时,我收到以下错误消息
Exception has occurred: ValueError
('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 1')
I have already read other threads regarding this error message but I could not fully understand and it and use the suggested solutions to sovle my issue.我已经阅读了有关此错误消息的其他线程,但我无法完全理解它并使用建议的解决方案来解决我的问题。 What do I do wrong?
我做错了什么?
You can use the apply function on a dataframe but also on an individual column.您可以在 dataframe 上使用应用 function,也可以在单个列上使用。 If you only need column B, you can use:
如果你只需要B列,你可以使用:
df['C'] = df['B'].apply(valInBitmap)
The function will receive the value from column B one by one and whatever the function returns will be saved as the value in C. function 将一一接收来自 B 列的值,无论 function 返回的值都将保存为 C 中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.