239-3 Dummy Codding

Screen Link:

Solution:

dummy_cols = pd.DataFrame()
for col in text_cols:
    dummy = pd.get_dummies(train[col])
    train = pd.concat([train, dummy], axis=1)
    del train[col]

What I expected to happen:
transform categorical to dummy

train = pd.get_dummies(train, columns=text_cols)

There is no error. But why use 5 lines and for loop instead of 1 line?

@Skydancer cant see the screen link. Could you also provide a link to the mission?

I am sorry, what is screen link?

The mission is 239-3 Dummy Codding

@Skydancer: I don’t have access to the course. Mind if you screenshot the question?

Annotation 2020-05-19 154515

1 Like

is get_dummies a function that you are able to use? I still need the question to figure out what you are referring to in your code.

I am asking if there is a reason for the solution to be not optimal. Am I missing something by doing it in 1 line only instead of 5?

if it works in one line then it should be more efficient than the one with 5 lines

Hi, @Skydancer! Long time no see :slight_smile:
You are correct, there are many places that could be done faster or with more efficient code. Take into consideration that this is educational platform where there are students with different levels of expertise. Sometimes they struggle to understand question, not speaking about the solution :wink: So some code is written in a way for people to understand the logic behind it. But some is just inefficient :wink:
And it is always fun to find ways to write faster and efficient code!
Cheers.

Thank you ranklord.
Indeed, long time. I had no time for Dataquest during MIT Introduction to Probability course + my business is sinking during COVID :))

1 Like