#๐Ÿ”’ How can i turn a categorical column into a binary one?

18 messages ยท Page 1 of 1 (latest)

zealous anvil
#

I am trying to turn the gender column into binary for so i can use it in linear regression but as seen in the photos instead of female being 1 and male 0 everything is either 0 or one

native jettyBOT
#

@zealous anvil

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pale tiger
#

!code

native jettyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

pale tiger
#

there are a thousand different ways to store tabular data, that screen snippet tells me very little about your code

zealous anvil
#

!code df_test["Gender"] = df_test["Gender"].where(df_test["Gender"] == "Male", 1) df_test["Gender"] = df_test["Gender"].where(df_test["Gender"] == "Female", 0) df_training["Gender"] = df_training["Gender"].where(df_training["Gender"] == "Male", 0) df_training["Gender"] = df_training["Gender"].where(df_training["Gender"] == "Female", 1)

pale tiger
#

copy the example code in the embed, replace print('hello world!') with yours

zealous anvil
#

is this better?

pale tiger
#

close, just need py after after the top 3 backticks

zealous anvil
#

i always forget how to do this

#

thx for the reminder

pale tiger
#

anyway, my pandas is pretty rusty but with vectorized operations normally you'd just make a new column that performs a criteria check

#

I think pandas also has an operation called... factorize?

#

aye, factorize will automatically enumerate unique values which should work for your operation

zealous anvil
#

ok will try it thanks

#

!close

native jettyBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.