-5

I try to compare two columns (string type) in dataframe :

if((MODEL_STANDARD_df['FT']== "4") and (MODEL_STANDARD_df['FT_CODE'] == ' ')):
    MODEL_STANDARD_df['ft2'] = "DIESEL"

but i get this error :

 Error in Python process: At line 18: <type 'exceptions.ValueError'>:
 The truth value of a Series is ambiguous. Use a.empty, a.bool(),
 a.item(), a.any() or a.all(). More info about this error

Any idea please to resolve this problem?

Thansk

butter
  • 73
  • 1
  • 7
  • Did you try searching with the error message - `The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().`? Were any of those results helpful? If any of those results answered your question, let us know which one and we will mark yours as a duplicate. – wwii Apr 07 '21 at 14:10
  • Does this answer your question? [How to conditionally update DataFrame column in Pandas](https://stackoverflow.com/questions/18196203/how-to-conditionally-update-dataframe-column-in-pandas) – tevemadar Apr 07 '21 at 14:15

3 Answers3

0

You have to extract the values from the series to create a boolean mask indicating where the conditions are simultaneously true in both columns using numpy's logical_and method:

import numpy as np

MODEL_STANDARD_df.loc[np.logical_and(MODEL_STANDARD_df['FT'].values=="4",MODEL_STANDARD_df['FT_CODE'].values==' '), 'ft2'] = "DIESEL"
tofd
  • 620
  • 4
  • 11
0

Return value of MODEL_STANDARD_df['FT']== "4" and MODEL_STANDARD_df['FT_CODE'] == ' ' is boolean series, you cann't use and on Series. you could use boolean indexing like:

MODEL_STANDARD_df['ft2'] = ""
MODEL_STANDARD_df.loc[ (MODEL_STANDARD_df['FT']== "4") & (MODEL_STANDARD_df['FT_CODE'] == ' '), 'ft2'] = "DIESEL"

Take the following dataframe as an example

FT    FT_CODE
4       3
4      ' '
4       2
4      ' '
5      ' '

(MODEL_STANDARD_df['FT']== "4") returns

0     True
1     True
2     True
3     True
4    False
Name: FT, dtype: bool

MODEL_STANDARD_df['FT_CODE'] == ' ' returns

0    False
1     True
2    False
3     True
4     True
Name: FT_CODE, dtype: bool

& has the same logic with and, which means if all True then True, so & of two boolean series is

0    False
1     True
2    False
3     True
4    False
dtype: bool

Boolean indexing means selecting only the True rows.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
0

You can use a apply function along your dataframe. See example below:

MODEL_STANDARD_df = pd.DataFrame({'FT': ["4", "1", "3"], "FT_CODE": [' ', '1', '4']})
MODEL_STANDARD_df['ft2'] = MODEL_STANDARD_df.apply(lambda x: "DIESEL" if x.FT == "4" and x.FT_CODE == " " else None, axis=1)

`

  • same problem : empty ft2 – butter Apr 07 '21 at 14:55
  • Let me see if I understood you. You want to create a third column in your datafram which if the FT column is 4 and FT_CODE is empty, the third column would be "DIESEL", right? And the others rows, what is the value when the condition is not reached? – cleobatista Apr 07 '21 at 21:44
  • Still blocked since 24H :( – butter Apr 08 '21 at 08:55
  • Sorry, I didn't undestand you. Who or what is still blocked? In pandas, you can't evaluate a dataframe column (ie, a pd.Series) to a single value. You can use a .loc, that check where in the dataframe your condition is reached, or use apply, that executes a function for each row, or column, in your dataframe. If your question is about this, please, say me. – cleobatista Apr 08 '21 at 18:21