What is the difference between parenthesis and no parenthesis in head and tail? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: What is the difference between parenthesis and no parenthesis in head and tail? (/thread-3305.html) |
What is the difference between parenthesis and no parenthesis in head and tail? - kulimer - May-12-2017 My first week using Python. I am a little confused about if I should use parenthesis or not when doing head and tail Suppose I have variable df as a data frame in NumPy df.head vs df.head() vs df.head(5)I understand the 5 means show first 5 rows, but how do I know if I should use df.head or df.head(), though df.head() gives me what I want. But, why the following. This would be correct df.columnThis df.column()would give me an error message. Why such inconsistency? I am so confused! RE: What is the difference between parenthesis and no parenthesis in head and tail? - nilamo - May-12-2017 Parenthesis are always required for functions/methods/instantiating objects. You're obviously using numpy, which does a few magic things with attribute lookup to help make working with large datasets easier (which means, for numpy, not using parenthesis is the right choice). RE: What is the difference between parenthesis and no parenthesis in head and tail? - kulimer - May-12-2017 Wait, you say "not using parenthesis is the right choice"? 1. df.head, gives me a bunch of data, NOT just the first few rows. It's not what I want! df.head() gives the first few rows. This is right. 2. df.column is right. df.column() wrong. 1 and 2 are both in NumPy, no? RE: What is the difference between parenthesis and no parenthesis in head and tail? - Larz60+ - May-13-2017 Maybe this will help you understand a bit more: def ziggy(): print('hello ziggy') def zaggy(): print('Hello zaggy, do you know ziggy?') xx = ziggy xx() xx= zaggy xx()results: using a function name without () returns the address of the function.therefore xx = ziggy assigns xx to the address of ziggy if you add parenthesis, the function will be executed, therefore in above example xx() will execute the function whose address is stored in it at the time it is called with () RE: What is the difference between parenthesis and no parenthesis in head and tail? - kulimer - May-14-2017 Thank you! It looks like Python still has that address and pointer stuff. I thought it is object-oriented. I think in Java you don't get this kind of address problem. In this day, who would want to know the address of the memory. We just want to get the work done. RE: What is the difference between parenthesis and no parenthesis in head and tail? - snippsat - May-14-2017 (May-14-2017, 01:35 AM)kulimer Wrote: Thank you! It looks like Python still has that address and pointer stuffThis has nothing to do about pointer stuff,that Python don't have. You can assign to df.columns = that's why it's not a function call.df.head() is just a function call to get rows,set to default 5. Eg: import pandas as pd df = pd.DataFrame({'a':[1,2,3,4], 'b': [10,20,30,40], 'c': [5,6,7,8], 'd': [50,60,70,80]}) # Set columns,would look bad with () df.columns = ['a', 'b', 'c', 'd'] print(df) a b c d 0 1 10 5 50 1 2 20 6 60 2 3 30 7 70 3 4 40 8 80 # caLL 2 rows print(df.head(2)) a b c d 0 1 10 5 50 1 2 20 6 60 kulime Wrote:I think in Java you don't get this kind of address problem.It's not a problem as explained, Java has it's own bundle of verbose stupidness RE: What is the difference between parenthesis and no parenthesis in head and tail? - Larz60+ - May-14-2017 I assure you, if Java runs on a computer, it uses addresses and pointers. You just don't see them as such, Python as well. Even if the code is stored as pcode, once it's compiled it will use them. A pointer is an indirect address, or an offset, which can be a register or a memory location the 'address' (since it is indirect) points to another memory address, or an offset from that address This allows changing what the 'pointer' points to by using replacement or some math Without them, tables or lists and other constructs would be very difficult to construct, and slow. RE: What is the difference between parenthesis and no parenthesis in head and tail? - volcano63 - May-14-2017 (May-14-2017, 07:04 AM)snippsat Wrote:Here's our hint - the guy comes from Java.(May-14-2017, 01:35 AM)kulimer Wrote: Thank you! It looks like Python still has that address and pointer stuffThis has nothing to do about pointer stuff,that Python don't have. Object attributes - which in C++ are called member variables (thankfully , I don't know Java equivalent) may be assigned and referenced directly, without setter /getter monstrosities Java heretics are used to .(May-14-2017, 01:35 AM)kulimer Wrote: In this day, who would want to know the address of the memory. We just want to get the work done.Python probably gives the best TTM - Time-To-Market - results. Much better than Java and C++. You just have to understand that it's neither.... (May-12-2017, 07:48 PM)kulimer Wrote: Suppose I have variable df as a data frame in NumPy df is an object
|