For example,
import pandas as pd
weather = pd.read_csv(r'D:\weather.csv')
weather.count
weather.count()
weather is one dataframe with multiple columns and rows Then what's the difference when asking for weather.count and weather.count() ?
For example,
import pandas as pd
weather = pd.read_csv(r'D:\weather.csv')
weather.count
weather.count()
weather is one dataframe with multiple columns and rows Then what's the difference when asking for weather.count and weather.count() ?
It depends. In general this question has nothing to do with pandas. The answer is relevant to how Python is designed.
In this case, .count
is a method. Particularly, a method of pandas.DataFrame
, and it will confirm it:
df = pd.DataFrame({'a': []})
print(df.count)
Outputs
<bound method DataFrame.count of Empty DataFrame
Columns: [a]
Index: []>
Adding ()
will call this method:
print(df.count())
Outputs
a 0
dtype: int64
However that is not always the case. .count
could have been a non-callable attribute (ie a string, an int, etc) or a property.
In this case it's a non-callable attribute:
class Foo:
def __init__(self, c):
self.count = c
obj = Foo(42)
print(obj.count)
Will output
42
Adding ()
in this case will raise an exception because it makes no sense to call an integer:
print(obj.count())
TypeError: 'int' object is not callable
The double bracket are related to a method call.
If you declare a function like this one that sum two number:
def sum(a:int, b:int)->int:
return a+b
Then you can call the function using: sum(1,2)
.
The function without bracket, are used when you don't need to call the function but, instead, you need to pass the reference to that function to another method.
For example, pass a reference to the function can be useful for multithread management:
from multiprocessing import Process
t = Process(target=my_long_running_function)
Please, have a look to the @roippi reply for further information: https://stackoverflow.com/a/21786508/9361998