For this you need to understand how variable scope works. Take a look at this!
def my_func():
index3 =5000
print(index3)
index3=10;
print(index3)
my_func()
output:
10
5000
Note: Even though there are two index3 you might think they are the same. But they are NOT
The index3 within the my_func is a local variable. While the one in your program (the one not in the function) index3 is different!. So what happens in the above code is that first print(index3) prints the index3 in my code (not in any functions..just in my program) then my_func() gets called and print(index3) within my_func() prints the local variable index3
Take a look at this!
def my_func():
print(index3)
index3=10;
print(index3)
my_func()
output:
10
10
See now both times the index3 which is same 10 this means it prints the global variable two times.
Now look:
def my_func():
index3 =index3+1
index3=10;
print(index3)
my_func()
output:
10
Traceback (most recent call last):
File "/home/mr/func.py", line 6, in <module>
my_func()
File "/home/mr/func.py", line 2, in my_func
index3 =index3+1
UnboundLocalError: local variable 'index3' referenced before assignment
Why?
Because of this index3 =index3+1
So the moment it sees a index3= it creates a local variable. So index3=0 means assign 0 to local variable.
However index3 =index3+1 would confuse it! It thinks
Wait you want me to assign local variable index3 as local variable index3+1 ? But you haven't even declared it yet!
def my_func():
global index3
index3 =index3+1
print(index3)
index3=10
print(index3)
my_func()
print(index3)
output:
10
11
11
Now it takes the global value within the function and it changes. So index3 is changed by the function.
NOTE: Using global variables is bad coding practice.
def getIndex3():
return index3
def my_func():
index3 = getIndex3()
index3 =index3+1
print(index3)
index3=10
print(index3)
my_func()
print(index3)
Now output:
10
11
10
So in your case.
def print_df_in_a_function():
print(df1)
This just resolves too the df1=None
in your program (Globally at the top). And doesn't mean the df1
your main.
However you can achieve what you want by passing the df1
(in main) to your
print_df_in_a_function(df1)
Now what happens is the df1
having value (your dataframe) will be passed to your print_df_in_a_function(df1):
and now you can print the value. Like this,
import numpy as np
import pandas as pd
df1 = None
def main():
df1 = pd.DataFrame(np.array([['a', 5, 9],
['b', 4, 61],
['c', 24, 9]]),
columns=['name', 'attr1', 'attr2'])
print(df1)
print_df_in_a_function(df1)
def print_df_in_a_function(df1):
print(df1)
if __name__ == '__main__':
main()
Output:
name attr1 attr2
0 a 5 9
1 b 4 61
2 c 24 9
name attr1 attr2
0 a 5 9
1 b 4 61
2 c 24 9
>>>