0

I am trying to read the csv file using pandas, while reading I want to read only few rows and few columns for that I can pass all the arguments like skiprows, nrows inside pd.read_csv. But I don't want to do this way I want to create a separate function and pass this function to pd.read_csv as shown below. Can anyone tell me how to achieve this.

import pandas as pd
file = r'C:\Users\Desktop\raw_data\Parameters.csv'

def readheader():
parse_config
{
    'nrows': 200,
    'header': [1,4],
    'skip_blank_lines': True
}
df = pd.read_csv(file, **parse_config)
print(df)
df.head()

1 Answer 1

1

You can do this:

def someFunction(filepath_or_buffer, skip_blank_lines):
  parse_config = {
      'filepath_or_buffer': filepath_or_buffer,
      'skip_blank_lines': skip_blank_lines
  }
  df = pd.read_csv(**parse_config)

someFunction(filepath_or_buffer='/some_path', skip_blank_lines=False)

keys in your parse_config object have to be valid parameters

Without modifying the pandas source code, you can't change what parameters are able to be passed to the read_csv method.

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work now. I got this error. ValueError: Invalid file path or buffer object type: <class 'dict'>
@atheesh27 without more info (your code, version of pandas, version of python) it's difficult to understand your issue.
Sorry ! it did work. I upvoted the answer.

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.