Babbbu95
Babbbu95

Reputation: 17

Using a Function parameter a variable to hold a dataframe

I am trying to assign new_df the dataframe but it says that new_df is not defined. is there a way that I can assign dataframe to the new_df variable using a function.

`def concat(file_xlsx, "sheet1", new_df):
         for f in file_xlsx:
             data=pd.read_excel(file_xlsx,"sheet1")
             new_df=pandas.Dataframe.append(data)
         return new_df`
        

Upvotes: 0

Views: 33

Answers (1)

Dipendra Pant
Dipendra Pant

Reputation: 395

# create an Empty DataFrame object 
new_df = pd.DataFrame()

def concat(file_xlsx, "sheet1", new_df):
    for f in file_xlsx:
        data=pd.read_excel(file_xlsx,"sheet1")
        # and then append data into it
        new_df=pandas.Dataframe.append(data)
        return new_df

You are appending data into a data frame without creating a data frame. First, you need to create a data frame(can be an empty data frame) and then append data on it. Please take a look into the above code snippet by me.

Upvotes: 1

Related Questions