mardi 25 février 2020

If statement over a DataFrame of strings

I'm trying to write a simple little if statement to return a value if a string is equal to the string at the ith position in the dataframe. print(chg_step) in the example below should output [3,3], but instead fails to run (ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().):

df = {'step_ID':[1,1,2,2,3,3,4,4], 'step_name':['CC_Dchg', 'CC_Dchg', 'Rest', 'Rest', 'CC_Chg', 
'CC_Chg', 'Rest', 'Rest']}
df = pd.DataFrame(df)

chg_step = []
a = []

for i in df:
    if df['step_name'] == 'CC_Chg':
        a = SiO_1['step_ID']
        chg_step = chg_step + a
    else:
        continue

What is the correct syntax to check if df['step_name'] == 'CC_Chg' is True?

Aucun commentaire:

Enregistrer un commentaire