Python Forum
change numerical values to categorical names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change numerical values to categorical names
#1
I am trying to convert numerical data points to categorical names in output. I want it to be labelled as categorical name but I need the raw data to stay numeric for functions elsewhere in the code. I have done this in anothe language (SAS) but I'm having trouble in python. I have found some code to clean up numbers (posted below). I do not get any errrors but it also does not change the output. Note. Please advise.

cleanup_nums = {"Congruency":     {1: 'Congruent', 2:'Incongruent'},
                "CueState": {1:'No Cue', 2: 'Center Cue', 3:'Spatial Cue' }}
Reply
#2
How can we help, if there is no code except the dictionary?
If you use pandas, you can do it easily, e.g.

In [75]: import pandas as pd
    ...:
    ...: mapper = {1: 'John', 2: 'July'}
    ...:
    ...: df = pd.DataFrame({'names': [1, 2, 1, 1], 'age': [10, 20, 10, 10]})
    ...:
    ...: df.names.map( mapper)
Output:
Out[75]: 0 John 1 July 2 John 3 John Name: names, dtype: object
Reply
#3
(Oct-24-2020, 07:41 PM)JoeOpdenaker Wrote: I am trying to convert numerical data points to categorical names in output. I want it to be labelled as categorical name but I need the raw data to stay numeric for functions elsewhere in the code. I have done this in anothe language (SAS) but I'm having trouble in python. I have found some code to clean up numbers (posted below). I do not get any errrors but it also does not change the output. Note. Please advise.

cleanup_nums = {"Congruency":     {1: 'Congruent', 2:'Incongruent'},
                "CueState": {1:'No Cue', 2: 'Center Cue', 3:'Spatial Cue' }}

I am not sure I understand your problem? Can you elaborate?
Reply
#4
Example without pandas.

cleanup_nums = {
    "Congruency": {
        1: 'Congruent', 2:'Incongruent',
    },
    "CueState": {
        1:'No Cue', 2: 'Center Cue', 3:'Spatial Cue',
    },
}


# (Congruency, CueState)
data = [
    (1,2),
    (2,1),
    (1,3),
    (1,1),
]


data_categories = []
for congruency, cuestate in data:
    congruency_name = cleanup_nums["Congruency"][congruency]
    cuestate_name = cleanup_nums["CueState"][cuestate]
    row = (congruency_name, cuestate_name)
    data_categories.append(row)

print(data_categories)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output values change akbarza 3 487 Oct-18-2023, 12:30 PM
Last Post: deanhystad
  Print names in x-axis of a time-series values hobbyist 4 1,177 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  restrict user input to numerical values MCL169 2 869 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 777 Jan-30-2023, 05:16 AM
Last Post: deanhystad
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,539 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Sorting numerical values provided by QAbstractTableModel BigMan 0 1,334 Jun-04-2022, 12:32 AM
Last Post: BigMan
  How to plot 3D graph of non numerical value? Gevni 0 2,186 Mar-05-2021, 02:50 PM
Last Post: Gevni
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 10,458 Feb-25-2020, 06:08 PM
Last Post: eddywinch82
  Using groupby on non-categorical values namy77 1 1,687 Feb-04-2020, 11:58 PM
Last Post: scidam
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,870 Oct-17-2019, 05:26 PM
Last Post: Smiling29

Forum Jump:

User Panel Messages

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