0

Alright, so my data is 1088 vehicles, split into 14 categories ("Hovedkategorinavn"). Each vehicle has a value describing how much it is used ("anvendelsesgrad"). I wish to copy the "anvendelsesgrad" variable, while changing the "anvendelsesgrad" value for some of the vehicle categories, but keeping the values for other categories. How can I do this?

I've tried to weird workarounds such as this ("inner_merge" being my dataframe, and the function "anvendelsesgrad" being the the function that calculates the variable "anvendelsesgrad"):

def anvend_ny6(var):

    if var == 'Entreprenørudstyr':
        return 1
    if var == 'Fejemaskiner, små':
        return 1
    if var == 'Fejemaskiner, store':
        return 1
    if var == 'Varevogne, komprimator':
        return 1
    if var == 'Transportvogne, små':
        return 1
    if var == 'Græsklipper':
        return 1
    if var == 'Personbiler':
        return 0.5
    if var == 'Gaffeltrucks':
        return 1
    else:
        return anvendelsesgrad(inner_merge.timer_tænding, inner_merge.tænding_agg, inner_merge.Count, inner_merge.dage_kørt_agg)

var3 = inner_merge.Hovedkategorinavn

inner_merge['nyanvend6'] = anvend_ny6(var3)
inner_merge.nyanvend6

However, this gives the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-241-bf7bd5cedfcf> in <module>
     20 var3 = inner_merge.Hovedkategorinavn
     21 
---> 22 inner_merge['nyanvend6'] = anvend_ny6(var3)
     23 inner_merge.nyanvend6

<ipython-input-241-bf7bd5cedfcf> in anvend_ny6(var)
      1 def anvend_ny6(var):
----> 2     if var == 'Entreprenørudstyr':
      3         return 1
      4     if var == 'Fejemaskiner, små':
      5         return 1

~\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1440     @final
   1441     def __nonzero__(self):
-> 1442         raise ValueError(
   1443             f"The truth value of a {type(self).__name__} is ambiguous. "
   1444             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

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

I'm confident there's a simpler solution than fixing this, and I am simply not knowledgeeable or smart enough to figure out how.

AKX
  • 152,115
  • 15
  • 115
  • 172

1 Answers1

0

You're trying to compare var3, a series, to a string.

If you want to call the function "row-wise",

inner_merge['nyanvend6'] = inner_merge.Hovedkategorinavn.apply(anvend_ny6)

and Pandas will call anvend_ny6 for each item in the series.

(However, that else clause calling anvendelsesgrad can still likely cause you some trouble.)

AKX
  • 152,115
  • 15
  • 115
  • 172