-1

I'm trying to create a function that takes a CSV input but when I try to input a csv like

function('sample.csv')

it only views the input as a string.

2
  • 2
    You'll have to open the CSV file to access its contents. Check out the documentation on csv files in python. Commented May 11, 2021 at 13:55
  • Does this answer your question? Reading data from a CSV file in Python Commented May 11, 2021 at 16:15

2 Answers 2

1

You're referencing a string in the function. You need to read the CSV file and then use it as an argument. With pandas it can be done as follows:

import pandas as pd

df = pd.read_csv('sample.csv')
function(df)
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood correctly what you're trying to do, something like this should do the trick:

import pandas as pd

def myfunc(csv_path):
    df = pd.read_csv(csv_path)
    return df

myfunc('sample.csv')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.