Skip to main content
deleted 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How can I make more efficient my read Reading and processing a file function?using Pandas

So, I am trying to read a file using pandas and then process it. For opening the file I use the following function:

import os
import pandas as pd

def read_base_file(data_folder, base_file):
    files = map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))
    if base_file in files:
        try:
            df = pd.read_csv(base_file, na_values=["", " ", "-"])
        except Exception, e:
            print "Error in reading", base_file
            print e
            df = pd.DataFrame()

    else:
        print "File Not Found."
        df = pd.DataFrame()
    return df

My main concerns are the ifif statement and what should I should return if there is an error.

How can I make more efficient my read file function?

So, I am trying to read a file using pandas and then process it. For opening the file I use the following function:

import os
import pandas as pd

def read_base_file(data_folder, base_file):
    files = map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))
    if base_file in files:
        try:
            df = pd.read_csv(base_file, na_values=["", " ", "-"])
        except Exception, e:
            print "Error in reading", base_file
            print e
            df = pd.DataFrame()

    else:
        print "File Not Found."
        df = pd.DataFrame()
    return df

My main concerns are the if statement and what should I return if there is an error.

Reading and processing a file using Pandas

I am trying to read a file using pandas and then process it. For opening the file I use the following function:

import os
import pandas as pd

def read_base_file(data_folder, base_file):
    files = map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))
    if base_file in files:
        try:
            df = pd.read_csv(base_file, na_values=["", " ", "-"])
        except Exception, e:
            print "Error in reading", base_file
            print e
            df = pd.DataFrame()

    else:
        print "File Not Found."
        df = pd.DataFrame()
    return df

My main concerns are the if statement and what I should return if there is an error.

Source Link

How can I make more efficient my read file function?

So, I am trying to read a file using pandas and then process it. For opening the file I use the following function:

import os
import pandas as pd

def read_base_file(data_folder, base_file):
    files = map(lambda x: os.path.join(data_folder, x), os.listdir(data_folder))
    if base_file in files:
        try:
            df = pd.read_csv(base_file, na_values=["", " ", "-"])
        except Exception, e:
            print "Error in reading", base_file
            print e
            df = pd.DataFrame()

    else:
        print "File Not Found."
        df = pd.DataFrame()
    return df

My main concerns are the if statement and what should I return if there is an error.