问题
I want do add a column to dataframe a,
a = pd.DataFrame([[1,2],[3,4]],columns=['A','B'])
if a['B'] > a['A']:
a['C']='是'
else:
a['C']='否'
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
回答1:
Use numpy.where:
#swapped 2,1
a = pd.DataFrame([[2,1],[3,4]],columns=['A','B'])
a['C'] = np.where(a['B']>a['A'], '是','否')
print (a)
A B C
0 2 1 否
1 3 4 是
Problem with your code is if use:
print (a['B']>a['A'])
0 False
1 True
dtype: bool
it return boolean mask and if cannot decide what to do.
Check also using if truth statements with pandas.
回答2:
Yes, where or numpy.select:
a = pd.DataFrame([[2,1],[3,4]],columns=['A','B'])
a['C'] = np.select([a['B']>a['A']], '是', default = '否')
print(a)
Returns:
A B C
0 2 1 否
1 3 4 是
Which easily scales to more conditions:
a = pd.DataFrame([[2,1],[3,4],[1,10]],columns=['A','B'])
condlist = [
a['B'] > 5*a['A'],
a['B'] > a['A']
]
valuelist = ['是', '否']
a['C'] = np.select(condlist, valuelist, default=np.nan)
print(a)
Returns:
A B C
0 2 1 nan
1 3 4 否
2 1 10 是
回答3:
Also a option is
np.choose()
Which is implemented in
np.choose('是','否')
来源:https://stackoverflow.com/questions/52735083/pandas-add-columns-note-the-truth-value-of-a-series-is-ambiguous