Python 3 lambda error: The truth value of a Series is ambiguous
Python 3 lambda error: The truth value of a Series is ambiguous
我收到此错误:The truth value of a Series is ambiguous
在我的 lambda 函数中。我知道这是关于此错误的非常全面的解释,但我认为这与我的问题无关:
基本上,我试图通过 lambda 确定 OpenBal
在同一个 AccountID 中从一个月到下个月是否相同,如果相同则给我一个“1”(例如,对于 OpenBal=101以下)。显然第一条记录应该给我一个 NaN。 (P.S。感谢@jdehesa 在我的另一个 post 中的回答)。
这证明了我的问题:
import pandas as pd
df = pd.DataFrame({'AccountID': [1,1,1,1,2,2,2,2,2],
'RefMonth': [1,2,3,4,1,2,3,4,5],
'OpenBal': [100,101,101,103,200,201,202,203,204]})
SameBal = df.groupby('AccountID').apply(lambda g: 1 if g['OpenBal'].diff() == 0 else 0)
df['SameBal'] = SameBal.sortlevel(1).values
1 if g['OpenBal'].diff() == 0
无效。这不是 pd.Series()
对象可以操作的方式
您需要创建一个合适的方法:
def convert(a):
return np.array([1 if i==0 else np.nan if pd.isnull(i) else 0 for i in a])
这将解决您的 The truth value of a Series is ambiguous
错误
SameBal = df.groupby('AccountID').apply(lambda g: pd.Series(data=convert(g['OpenBal'].diff().values), index=g['RefMonth']))
SameBal.name = 'SameBal'
SameBal
Out[]:
AccountID RefMonth
1 1 NaN
2 0.0
3 1.0
4 0.0
2 1 NaN
2 0.0
3 0.0
4 0.0
5 0.0
df.merge(SameBal.reset_index())
Out[]:
AccountID OpenBal RefMonth SameBal
0 1 100 1 NaN
1 1 101 2 0.0
2 1 101 3 1.0
3 1 103 4 0.0
4 2 200 1 NaN
5 2 201 2 0.0
6 2 202 3 0.0
7 2 203 4 0.0
8 2 204 5 0.0
你的错误正确地表明你无法检查系列的真实性。但此任务不需要自定义匿名函数。
使用 groupby
+ transform
与 pd.Series.diff
:
import pandas as pd
df = pd.DataFrame({'AccountID': [1,1,1,1,2,2,2,2,2],
'RefMonth': [1,2,3,4,1,2,3,4,5],
'OpenBal': [100,101,101,103,200,201,202,203,204]})
df['A'] = (df.groupby('AccountID')['OpenBal'].transform(pd.Series.diff)==0).astype(int)
print(df)
AccountID OpenBal RefMonth A
0 1 100 1 0
1 1 101 2 0
2 1 101 3 1
3 1 103 4 0
4 2 200 1 0
5 2 201 2 0
6 2 202 3 0
7 2 203 4 0
8 2 204 5 0
如果每组第一行需要NaN
:
g = df.groupby('AccountID')['OpenBal'].transform(pd.Series.diff)
df['A'] = (g == 0).astype(int)
df.loc[g.isnull(), 'A'] = np.nan
print(df)
AccountID OpenBal RefMonth A
0 1 100 1 NaN
1 1 101 2 0.0
2 1 101 3 1.0
3 1 103 4 0.0
4 2 200 1 NaN
5 2 201 2 0.0
6 2 202 3 0.0
7 2 203 4 0.0
8 2 204 5 0.0
我收到此错误:The truth value of a Series is ambiguous
在我的 lambda 函数中。我知道这是关于此错误的非常全面的解释,但我认为这与我的问题无关:
基本上,我试图通过 lambda 确定 OpenBal
在同一个 AccountID 中从一个月到下个月是否相同,如果相同则给我一个“1”(例如,对于 OpenBal=101以下)。显然第一条记录应该给我一个 NaN。 (P.S。感谢@jdehesa 在我的另一个 post 中的回答)。
这证明了我的问题:
import pandas as pd
df = pd.DataFrame({'AccountID': [1,1,1,1,2,2,2,2,2],
'RefMonth': [1,2,3,4,1,2,3,4,5],
'OpenBal': [100,101,101,103,200,201,202,203,204]})
SameBal = df.groupby('AccountID').apply(lambda g: 1 if g['OpenBal'].diff() == 0 else 0)
df['SameBal'] = SameBal.sortlevel(1).values
1 if g['OpenBal'].diff() == 0
无效。这不是 pd.Series()
对象可以操作的方式
您需要创建一个合适的方法:
def convert(a):
return np.array([1 if i==0 else np.nan if pd.isnull(i) else 0 for i in a])
这将解决您的 The truth value of a Series is ambiguous
错误
SameBal = df.groupby('AccountID').apply(lambda g: pd.Series(data=convert(g['OpenBal'].diff().values), index=g['RefMonth']))
SameBal.name = 'SameBal'
SameBal
Out[]:
AccountID RefMonth
1 1 NaN
2 0.0
3 1.0
4 0.0
2 1 NaN
2 0.0
3 0.0
4 0.0
5 0.0
df.merge(SameBal.reset_index())
Out[]:
AccountID OpenBal RefMonth SameBal
0 1 100 1 NaN
1 1 101 2 0.0
2 1 101 3 1.0
3 1 103 4 0.0
4 2 200 1 NaN
5 2 201 2 0.0
6 2 202 3 0.0
7 2 203 4 0.0
8 2 204 5 0.0
你的错误正确地表明你无法检查系列的真实性。但此任务不需要自定义匿名函数。
使用 groupby
+ transform
与 pd.Series.diff
:
import pandas as pd
df = pd.DataFrame({'AccountID': [1,1,1,1,2,2,2,2,2],
'RefMonth': [1,2,3,4,1,2,3,4,5],
'OpenBal': [100,101,101,103,200,201,202,203,204]})
df['A'] = (df.groupby('AccountID')['OpenBal'].transform(pd.Series.diff)==0).astype(int)
print(df)
AccountID OpenBal RefMonth A
0 1 100 1 0
1 1 101 2 0
2 1 101 3 1
3 1 103 4 0
4 2 200 1 0
5 2 201 2 0
6 2 202 3 0
7 2 203 4 0
8 2 204 5 0
如果每组第一行需要NaN
:
g = df.groupby('AccountID')['OpenBal'].transform(pd.Series.diff)
df['A'] = (g == 0).astype(int)
df.loc[g.isnull(), 'A'] = np.nan
print(df)
AccountID OpenBal RefMonth A
0 1 100 1 NaN
1 1 101 2 0.0
2 1 101 3 1.0
3 1 103 4 0.0
4 2 200 1 NaN
5 2 201 2 0.0
6 2 202 3 0.0
7 2 203 4 0.0
8 2 204 5 0.0