Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have used the below function without giving the parameters and this function worked fine and could export the values to CSV, but when I tried using the function by passing parameters as shown below it is not export the values to CSV. There is no error and the function returns 0 

def grp(department, group):

    df = pd.DataFrame(data=students, columns=['Name', 'Department', 'Script'])

    df = df[df.Department == department]

    df['Script'] = 'ADD USER ' + df['Name'] + ' TO ' + group

    return df['Script']

with open('output.csv', 'w', newline='') as csvfile:

    writer = csv.writer(csvfile)

    for val in grp('Biology', 'Science'):

        writer.writerow([val])

Can anyone tell me what is happening?

1 Answer

0 votes
by (36.8k points)

You are using a loop which is not required. The function return Pandas series just use to_csv() to export. Check the code below:

grp('Biology', 'Science').to_csv('output.csv')Python for loop on function with args for csv export

I have used the below function without giving the parameters and this function worked fine and could export the values to CSV, but when I tried using the function by passing parameters as shown below it is not export the values to CSV. There is no error and the function returns 0 

def grp(department, group):

    df = pd.DataFrame(data=students, columns=['Name', 'Department', 'Script'])

    df = df[df.Department == department]

    df['Script'] = 'ADD USER ' + df['Name'] + ' TO ' + group

    return df['Script']

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch 

Browse Categories

...