Python Forum
Assigning conditional values in Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning conditional values in Pandas
#1
Hi everyone,

I am playing around with some old code from online and am getting an error:

for dataset in train_test_data:
    dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0,
    dataset.loc[(dataset['Age'] > 16) & (dataset['Age'] <= 26), 'Age'] = 1,
    dataset.loc[(dataset['Age'] > 26) & (dataset['Age'] <= 36), 'Age'] = 2,
    dataset.loc[(dataset['Age'] > 36) & (dataset['Age'] <= 62), 'Age'] = 3,
    dataset.loc[ dataset['Age'] > 62, 'Age'] = 4
ValueError: Must have equal len keys and value when setting with an iterable

Does anyone know how to assign conditional values in pandas?

Thanks
Scott
Reply
#2
you must elaborate, there's not enough information here.
what are the error codes?
what does train_test_data structure look like, etc.
Also, code needs to be a snippet that can be run, or at least, one where all inputs and outputs are visable.
Reply
#3
Hi,

here is the complete code in github. It was written by someone years ago and I'm guessing there has been some changes to python since then.

https://github.com/minsuk-heo/kaggle-tit...tion.ipynb

If you run this script you'll get the same error at the point in code above.

Thanks
Reply
#4
This code was written using Iron python link here
There shouldn't be any massive changes since 2017.
I present this link as an aid.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xlswriter(pandas) and conditional format paul18fr 1 1,170 Aug-28-2022, 07:56 AM
Last Post: paul18fr
  pandas: Compute the % of the unique values in a column JaneTan 1 1,791 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,328 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas: summing columns conditional on the column labels ddd2332 0 2,145 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Assigning Column nunique values to another DataFrame column Pythonito 0 1,725 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Pandas + Groupby + Filter unique values JosepMaria 1 2,889 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  Python pandas merge with or conditional Lafayette 0 2,196 May-07-2020, 07:34 PM
Last Post: Lafayette
  Pandas copying wrong values vmarg 2 2,407 Jan-06-2020, 09:45 AM
Last Post: vmarg
  sort values of a column pandas karlito 2 2,530 Oct-22-2019, 06:11 AM
Last Post: karlito
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,688 Jul-16-2019, 08:13 AM
Last Post: ptaylor520

Forum Jump:

User Panel Messages

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