-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.

jt25
  • 3
  • 1
  • 2
    You'll have to open the CSV file to access its contents. Check out the documentation on [csv files](https://docs.python.org/3/library/csv.html#csv.reader) in python. – Cubed May 11 '21 at 13:55
  • Does this answer your question? [Reading data from a CSV file in Python](https://stackoverflow.com/questions/26903304/reading-data-from-a-csv-file-in-python) – Ganesh Tata May 11 '21 at 16:15

2 Answers2

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)
CodeKorn
  • 300
  • 1
  • 8
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')
Leonardo Viotti
  • 476
  • 1
  • 5
  • 15