Qavi10
Qavi10

Reputation: 13

Add 2 data files in pyinstaller and use them as variables in the function written in the script

My aim is to create a one executable file using pyinstaller by converting a .py or .ipynb file. I'd like the user to enter their excel file pathway(2 files) when they run the exe file in their computer. The exe should then run my written python code which takes in their data files entered as my function arguments for data manipulation purpose and return's my function output(manipulated csv file) to the end-user using this application.

def manipulated_data(df,df5,df10):
    df2=df.copy()


    df4=df2.pivot_table(columns=df2[df2.columns[6]],index=df2[np.r_[df2.columns[0:6],df2.columns[9:17],df2.columns[18:]]],values=df2[df2.columns[7:9]],fill_value='')

    df4=df4.reset_index()

    #Combining multi-index names
    df4.columns = [' '.join(col).strip() for col in df4.columns.values]




    df7=df5.melt(id_vars=df5[df5.columns[0:5]],value_vars=df5[df5.columns[6:15]],value_name='Values')

    df7=df7.replace('Zone 9`','Zone 9')

    ind=df7.variable.unique()

    df8=df7.pivot_table(columns=df7[df7.columns[4:6]],
                    index=df7[df7.columns[0:4]]
                    , values=df7.columns[6],aggfunc=('first'),fill_value='')

    df8=df8.reindex(ind,axis=1,level=1)

    df8.columns = [' '.join(col).strip() for col in df8.columns.values]

    df8.reset_index(inplace=True)

    df9=pd.merge(df4,df8,how='inner',on=['Store ID','Store Name','State'])

    df9.fillna('',inplace=True)

    dfcoles=pd.concat([df9,df10], axis=0,ignore_index=False,sort=False)

    dfcoles.fillna('',inplace=True)

    coles_ind=df10.columns

    dfcoles=dfcoles.reindex(coles_ind,axis=1)

    return dfcoles

df_trial=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Day 14 data/Part 3.csv',keep_default_na=False)

df_part4=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Day 14 data/Part 4.csv',keep_default_na=False)

df_coles=pd.read_csv('C:/Users/Abdul.Qavi/Downloads/Coles Service SF.csv',keep_default_na=False)
 ##This file wouldn't be entered by the user. It would always be a part of the script.

dfx=manipulated_data(df_trial,df_part4,df_coles)

Upvotes: 0

Views: 508

Answers (1)

nagaraj bhat
nagaraj bhat

Reputation: 124

Based on my understanding of the question

  • you want the user to input the path for 2 csv files
  • read the files and pass it as an argument
  • perform some operations using manipulated_data() function
  • return/save the output - say in a csv file format.

The general version would look like this -

app.py

import pandas as pd
import os

def manipulated_data(df1,df2):
    #perform operations using df1 and df2
    #......
    #......
    #for example - 
    df_coles = pd.concat([df1,df2], axis=0,ignore_index=False,sort=False) 
    return df_coles

# get file paths from the user
path1 = input("enter path for the 1st file")
path2 = input("enter path for the 2nd file")

#read the csv files
data1 = pd.read_csv(path1)
data2 = pd.read_csv(path2)

#execute manipulated_data() function-
df_final = manipulated_data(data1,data2)

#create an output directory
if not os.path.exists('output'):
    os.makedirs('output')

#save csv file in the output directory or your prefered location
df_final.to_csv('./output/manipulated_data1.csv') 

# Open excel to display the csv file
os.system("start excel output/manipulated_data1.csv")

# To prevent the app from closing abruptly -
input("Press enter to continue...")

And then create the application -

pyinstaller app.py

Side note - You can use os.getcwd() to read files from or save the file to the current directory.

Upvotes: 1

Related Questions