So, I have this script in python using pandas that does a few things. It: combines two excel sheets together and makes a new one and it also adds another column to those sheets that shows where the original file came from. Here is the script:
import pandas as pd
import numpy as np
import os
from os.path import basename
df = []
#enter your file names via terminal
file1 = raw_input("Enter the path to the first file):")
file2 = raw_input("Enter the path to the second file):")
for f in [file1, file2]:
data = pd.read_excel(f, 'Sheet1')
data.index = [os.path.basename(f)] * len(data)
df.append(data)
#add the column that includes the original file
data.index = [basename(f)] * len(data)
#set the path and name of your final product file
final = raw_input('Where do you want the file, and what do you want to name it? (C:\path_to_file\name_of_file.xlsx):')
df = pd.concat(df)
df.to_excel(final)
Now, my question is, let's say we combine two excel files, such that they look like this:
Item Inv Price Sold
dbtest1.xlsx Banana 50 1 27
dbtest1.xlsx Grapes 100 3 68
dbtest2.xlsx Oranges 68 3 17
dbtest2.xlsx Apples 22 1.5 9
dbtest2.xlsx Strawberries 245 4 122
And I want to add this excel file, now called dbtestfinal.xlsx to another excel file. The results I'd get are:
Item Inventory Price Sold
dbtest3.xlsx Pork 49 2.99 47
dbtest3.xlsx Beef 27 1.5 78
dbtest3.xlsx Chicken 245 1.99 247
dbtestfinal.xlsx Banana 50 1 27
dbtestfinal.xlsx Grapes 100 3 68
dbtestfinal.xlsx Oranges 68 3 17
dbtestfinal.xlsx Apples 22 1.5 9
dbtestfinal.xlsx Stra... 245 4 122
I'd like it to be able to maintain the original files it came from, so instead of having just dbtest3.xlsx and dbtestfinal.xlsx, it would have dbtest1,2,3 instead. Is there a way to make it do such a thing?
Also, adding in a column for the date in which the file was added would be great, too!
And one last addition, and this one is likely not trivial: is there a way to have the program detect the same file origin and replace it with the new one? So if you edited dbtest2.xlsx and added/subtracted items, the program would remove the old ones and only input this new file?
Thank you for any suggestions!