Posts: 3
Threads: 1
Joined: May 2017
May-12-2017, 07:48 PM
(This post was last modified: May-12-2017, 07:48 PM by kulimer.)
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!
Posts: 3,458
Threads: 101
Joined: Sep 2016
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).
Posts: 3
Threads: 1
Joined: May 2017
May-12-2017, 09:50 PM
(This post was last modified: May-12-2017, 09:51 PM by kulimer.)
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?
Posts: 12,023
Threads: 484
Joined: Sep 2016
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 ()
Posts: 3
Threads: 1
Joined: May 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.
Posts: 7,312
Threads: 123
Joined: Sep 2016
May-14-2017, 07:04 AM
(This post was last modified: May-14-2017, 12:54 PM by snippsat.)
(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
Posts: 12,023
Threads: 484
Joined: Sep 2016
May-14-2017, 12:19 PM
(This post was last modified: May-14-2017, 12:19 PM by Larz60+.)
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.
Posts: 566
Threads: 10
Joined: Apr 2017
May-14-2017, 11:53 PM
(This post was last modified: May-14-2017, 11:53 PM by volcano63.)
(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  Java.
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
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.
|