Python Forum
Changing a string value to a numerical value using python code and a lamda function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing a string value to a numerical value using python code and a lamda function
#7
I think you are missing that df is a dataframe. This does not work because df["State"] is a series, not an element of a list or value in a dictionary.
import pandas as pd
df = pd.DataFrame({"State": ["NORMAL", "BROKEN"]})
df["label"] = 0 if df["State"] == "NORMAL" else 1
Error:
Traceback (most recent call last): File "...", line 3, in <module> df["label"] = 0 if df["State"] == "NORMAL" else 1 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
"put your one-liner in some kind of loop" is essentially what "map()" and apply() are doing
Reply


Messages In This Thread
RE: Changing a string value to a numerical value using python code and a lamda function - by deanhystad - Jul-05-2022, 11:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 407 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  restrict user input to numerical values MCL169 2 953 Apr-08-2023, 05:40 PM
Last Post: MCL169
Question Inserting Numerical Value to the Element in Optionlist and Printing it into Entry drbilgehanbakirhan 1 846 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Code changing rder of headers Led_Zeppelin 0 925 Jul-13-2022, 05:38 PM
Last Post: Led_Zeppelin
  Sorting numerical values provided by QAbstractTableModel BigMan 0 1,390 Jun-04-2022, 12:32 AM
Last Post: BigMan
  Convert a string to a function mikepy 8 2,593 May-13-2022, 07:28 PM
Last Post: mikepy
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,184 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,319 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  changing Python files to .exe alok 2 2,271 Jul-20-2021, 02:49 PM
Last Post: alok
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 2,023 Apr-05-2021, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020