I want to do something like:
if table['STATUS']=='A'or 'P':
table['END_DATE']=end_date
In order to replace the value of END_DATE for those rows that have a S or P in STATUS Which yields the
ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().
I get that it is due to the array adressing... This seems to be a petty common issue but I can't seem to find a suitable answer what is the Right constuct to use here? I can't figure it out from the docs...which say to use .any()
The if statement is ambiguous because it evaluates to a Series that maybe be empty, maybe have have some True values or False values. Hence its impossible to evaluate. See here: http://pandas.pydata.org/pandas-docs/dev/gotchas.html#using-if-truth-statements-with-pandas , http://pandas.pydata.org/pandas-docs/dev/indexing.html#boolean-indexing , http://pandas.pydata.org/pandas-docs/dev/basics.html#flexible-comparisons
This works in 0.12
In [2]: df = DataFrame(np.arange(20).reshape(10,2),columns=['A','B'])
In [3]: df['status'] = ['A']*4 + ['invalid'] * 2 + ['P'] * 4
This step won't be needed in 0.13
In [4]: df['end_date'] = np.nan
Edit from @DSM
In [5]: df.loc[(df['status'].isin(['A','P'])),'end_date'] = Timestamp('20130101')
In [6]: df
Out[6]:
A B status end_date
0 0 1 A 2013-01-01 00:00:00
1 2 3 A 2013-01-01 00:00:00
2 4 5 A 2013-01-01 00:00:00
3 6 7 A 2013-01-01 00:00:00
4 8 9 invalid NaN
5 10 11 invalid NaN
6 12 13 P 2013-01-01 00:00:00
7 14 15 P 2013-01-01 00:00:00
8 16 17 P 2013-01-01 00:00:00
9 18 19 P 2013-01-01 00:00:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.