Home > database >  Pandas update column conditionally if another column exists
Pandas update column conditionally if another column exists

Time:08-24

I have a dataframe with a column whose value depends on the existence of another column, so I tried to use np.where to condition the value like this:

the_dataframe["dependant_value"] = np.where(
    "independant_value_"   the_dataframe["suffix"].str.lower()
    in the_dataframe.columns,
    the_dataframe["another_column"]
    * the_dataframe[
        "independent_value_"   the_dataframe["suffix"].str.lower()
    ],
    0,
)

But I'm getting this error:

 File "C:\the_file.py", line 271, in the_method
    "independent_value_"   the_dataframe["suffix"].str.lower()
  File "C:\the_file.py", line 4572, in __contains__
    hash(key)
TypeError: unhashable type: 'Series'

I suppose there must be a propper way to make the logic evaluation of the condition, but I haven't found it.

CodePudding user response:

There is no syntax like Series in Series/Index

$ s = pd.Series([1, 2])
$ s in s

Traceback (most recent call last):
  File "~/sourcecode/test/so/73460893.py", line 44, in <module>
    s in s
  File "~/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 1994, in __contains__
    return key in self._info_axis
  File "~/.local/lib/python3.10/site-packages/pandas/core/indexes/range.py", line 365, in __contains__
    hash(key)
TypeError: unhashable type: 'Series'

You might want Series.isin

    ("independant_value_"   the_dataframe["suffix"].str.lower()).isin(the_dataframe.columns),
  • Related