I have function in Python Pandas like below:
def fk(path):
if path:
df = pd.read_csv(f"{path}")
else:
client_type = str(input())
if client_type == "A":
df= pd.read_csv("Data/Client_A")
elif client_type == "B":
df= pd.read_csv("Data/Client_B")
My idea of above function is:
- If I give path to the file (path parameter of cuntion) to my function take dataset from this path.
- If the path condition is empty choose client type and if you choose "A" take dataset from:
df= pd.read_csv("Data/Client_A")
if you choose "B" take dataset fromdf= pd.read_csv("Data/Client_B")
How can I repare this function because if path:
does not work, could you modify my function based on my description above ?
CodePudding user response:
You need to use a default argument:
def fk(path=None): ### EDITED THIS LINE
if path:
df = pd.read_csv(f"{path}")
else:
client_type = str(input())
if client_type == "A":
df= pd.read_csv("Data/Client_A")
elif client_type == "B":
df= pd.read_csv("Data/Client_B")
This automatically sets path
to None
if no argument is passed to fk()
.