How does dataframe assignment work internally? /u/throwaway84483994 Python Education

I have been watching this tutorial on ML by freecodecamp. At timestamp 7:18 the instructor assigns values to a DataFrame column 'class' in one line with the code:

df["class"] = (df["class"] == "g").astype(int)

I understand what the above code does—i.e., it converts each row in the column 'class' to either 0 or 1 based on the condition: whether the existing value of that row is "g" or not.

However, I don’t understand how it works. Is (df["class"] == "g") a shorthand for an if condition? And even if it is, why does it work with just one line of code when there are multiple existing rows?

Can someone please help me understand how this works internally? I come from a Java and C++ background, so I find it challenging to wrap my head around some of Python’s ‘shortcuts’.

submitted by /u/throwaway84483994
[link] [comments]

​r/learnpython I have been watching this tutorial on ML by freecodecamp. At timestamp 7:18 the instructor assigns values to a DataFrame column ‘class’ in one line with the code: df[“class”] = (df[“class”] == “g”).astype(int) I understand what the above code does—i.e., it converts each row in the column ‘class’ to either 0 or 1 based on the condition: whether the existing value of that row is “g” or not. However, I don’t understand how it works. Is (df[“class”] == “g”) a shorthand for an if condition? And even if it is, why does it work with just one line of code when there are multiple existing rows? Can someone please help me understand how this works internally? I come from a Java and C++ background, so I find it challenging to wrap my head around some of Python’s ‘shortcuts’. submitted by /u/throwaway84483994 [link] [comments] 

I have been watching this tutorial on ML by freecodecamp. At timestamp 7:18 the instructor assigns values to a DataFrame column 'class' in one line with the code:

df["class"] = (df["class"] == "g").astype(int)

I understand what the above code does—i.e., it converts each row in the column 'class' to either 0 or 1 based on the condition: whether the existing value of that row is "g" or not.

However, I don’t understand how it works. Is (df["class"] == "g") a shorthand for an if condition? And even if it is, why does it work with just one line of code when there are multiple existing rows?

Can someone please help me understand how this works internally? I come from a Java and C++ background, so I find it challenging to wrap my head around some of Python’s ‘shortcuts’.

submitted by /u/throwaway84483994
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *