0

Tried to retrieve Cost, if s['O_Status'] values is Closed, using below code.

Got this error, ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

.

 FClose = [i for i in s['Cost'] if s['O_Status'] == 'Closed']

    Cost      Year      O_Status ----> data frame column name
-------------------------------
6100000   2001      Closed
100004    2009      Operating
2004000   2015      Closed
144007    1999      Operating

Also is it possible to make the Categorical variable values Closed and Operating in to a new dataframe in the below format and store relative cost values,

Closed     Operating ------> data frame column name
--------------------------    
6100000    100004    
2004000    144007      
xojiv
  • 15
  • 2
  • `s['O_Status'] == 'Closed'` is a Series of True/False Values. The Error is telling you it's ambiguous because what does `if pd.Series([True, False, True, False])` mean? It's expecting `if True` or `if False` not `if complex object`, which makes no sense – ALollz Feb 24 '21 at 16:31
  • Does this answer your question? [How to select rows from a DataFrame based on column values](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Burrito Feb 24 '21 at 18:37

1 Answers1

0
import io

df = pd.read_csv(io.StringIO('''Cost      Year      O_Status
6100000   2001      Closed
100004    2009      Operating
2004000   2015      Closed
144007    1999      Operating'''), sep='\s+', engine='python')

FClose = df[df['O_Status'] == 'Closed']['Cost'].tolist()
print(FClose)

FOp = df[df['O_Status'] == 'Operating']['Cost'].tolist()
print(FOp)

dfnew = pd.concat([pd.DataFrame(FClose, columns=['Closed']), pd.DataFrame(FOp, columns=['Operating'])], axis=1)

Output

    Closed  Operating
0   6100000 100004
1   2004000 144007
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14