Recoding Categorical to Numerical

2021-08-16 Thread John Griner
thanks


Virus-free.
www.avast.com

<#m_4567368322047287300_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Recoding Categorical to Numerical

2021-08-14 Thread Peter Otten

On 14/08/2021 00:33, John Griner wrote:

Hello, and thanks in advance,

  I am trying to recode categorical variable to numeric.  Despite using
lambda, If else, dummy-recoding, and so forth, what I get is from ONE
column that has 4 variables (City, Town, Suburb, Rural), I get FOUR columns:



localeDummy_City  localeDummy_Town  localeDummy_Suburb
localeDummy_Rural   locale_recode



with the corresponding numeric variable.



What I want is the recode to have ONE column that has the numeric
conversion.



For instance:


local_recode

2

4

4

6

2

8

6

2

8

2

2

4

6

4

8

and so forth, where I have set City to 2, and Town to 4, etc.


Again, thanks, John


My crystal ball says you want

import pandas

df = pandas.DataFrame(
[
[("City", "Suburb")],
[("Town", "City", "Suburb")],
[("Rural",)]
],
columns=["before"]
)

flags = dict(
City=1,
Town=2,
Suburb=4,
Rural=8
)

df["after"] = df["before"].apply(
lambda names: sum(flags[name] for name in set(names))
)

print(df)

If that's not it show us your failing code, preferably as a small 
self-contained script that also generates the required input data. Use 
cut and paste, and include it into the message body as attachments are 
usually removed.



--
https://mail.python.org/mailman/listinfo/python-list


Recoding Categorical to Numerical

2021-08-13 Thread John Griner
Hello, and thanks in advance,

 I am trying to recode categorical variable to numeric.  Despite using
lambda, If else, dummy-recoding, and so forth, what I get is from ONE
column that has 4 variables (City, Town, Suburb, Rural), I get FOUR columns:



localeDummy_City  localeDummy_Town  localeDummy_Suburb
localeDummy_Rural   locale_recode



with the corresponding numeric variable.



What I want is the recode to have ONE column that has the numeric
conversion.



For instance:


local_recode

2

4

4

6

2

8

6

2

8

2

2

4

6

4

8

and so forth, where I have set City to 2, and Town to 4, etc.


Again, thanks, John



loca




Virus-free.
www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-- 
https://mail.python.org/mailman/listinfo/python-list