Python Forum
What is the difference between parenthesis and no parenthesis in head and tail?
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference between parenthesis and no parenthesis in head and tail?
#1
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.column
This
df.column()
would give me an error message.

Why such inconsistency? I am so confused!
Reply
#2
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).
Reply
#3
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?
Reply
#4
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:
Output:
hello ziggy Hello zaggy, do you know ziggy?
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 ()
Reply
#5
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.
Reply
#6
(May-14-2017, 01:35 AM)kulimer Wrote: Thank you! It looks like Python still has that address and pointer stuff
This 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 Wink
Reply
#7
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.
Reply
#8
(May-14-2017, 07:04 AM)snippsat Wrote:
(May-14-2017, 01:35 AM)kulimer Wrote: Thank you! It looks like Python still has that address and pointer stuff
This 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.
Here's our hint - the guy comes from  Naughty Java.

Object attributes - which in C++ are called member variables (thankfully  Tongue , I don't know Java equivalent) may be assigned and referenced directly, without setter/getter monstrosities Java heretics are used to  Hand .

(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  head() d8a988 4 2,589 Sep-03-2020, 09:08 AM
Last Post: buran
  pandas head() not reading all rows naab 0 1,777 Apr-07-2020, 01:06 PM
Last Post: naab

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020