0

NOTE StackOverflow does not recognize my multi-line code and complains that it needs 4 indents (provided), so I had to convert multi-line code to blockquotes.

First, I'm reading columns from a dataframe where I want to extract some of the column values. In one case, I want to extract two column values in a list within a list and 1 just 1 column value (this is done).

The issue: I'm taking in a parameter that represents multiple several columns in a dataframe - in this case my2Columns:

def myFunction(myFile,my2Columns)

I read the file using pandas df = pd.read_csv(myFile) and from here need to extract 2 columns within this df:

twoCols = df[['Col1', 'Col2']] ###hardcoing like this will work, but it's not dynamic

Because the my2Columns parameter should be formatted like ['Col1', 'Col2'], I thought creating the function and calling the function like this would work - but it doesn't and I get a key error:

def myFunction(myFile,my2Columns) df = pd.read_csv(myFile) twoCols = df[my2Columns] return twoCols

print(myFunction("myfile.csv","['Col1','Col2']")) #Same error: #print(myFunction("myfile.csv","'Col1','Col2'"))

Another technique I tried was converting it to a list after extraction:

def myFunction(myFile,my2Columns) df = pd.read_csv(myFile) betweenConversion = df[my2Columns] twoCols = betweenConversion.tolist() return twoCols

Same key error here. How do I pass in 2 columns for pandas to read from a file and store in 1 variable in the format of [[Col1,Col2] with the values such as [[1,1],[2,2],[3,3]]?

RMDLtz
  • 1

0 Answers0