Method chaining without parentheses

Lesson 8/14 in Data Cleaning with Pandas says that I could use method chaining by:

laptops["cpu_manufacturer"] = (laptops["cpu"].str.split().str[0])

However, when I use the same code without the parentheses:

laptops["cpu_manufacturer"] = laptops["cpu"].str.split().str[0]

I get the same answer. Is the parentheses just a formality, or does it actually affect the code in some cases?

Hey.

We’re not really doing what you claim we’re doing in that screen. Near the middle of the screen we can read the following.

This method splits each string on the whitespace; the result is a series containing individual Python lists. Also note that we used parentheses to method chain over multiple lines, which makes our code easier to read.

This justifies usages like this:

laptops["cpu_manufacturer"] = (laptops["cpu"]
                                       .str.split()
                                       .str[0]
                              )

In the same line, the parentheses aren’t necessary, Python just ignores them because they’re superfluous here. Here’s an extreme example:

>>> print((((3))))
3

I hope this helps.

Hi Bruno,

Thanks for the reply. I can’t read apparently :frowning:

1 Like