I have about (100 files +) XLSX files in a folder with different columns names and data types
File 1:
Id test category
1 ab 4
2 cs 3
3 cs 1
FILE 2:
index remove stocks category
1 dr 4 a
2 as 3 b
3 ae 1 v
File 3: ....
File 4.....
This is my try based on another example:
# current directory (including python script & all excel files)
mydir = (os.getcwd()).replace('\\','/') + '/'
#Get all excel files include subdir
filelist=[]
for path, subdirs, files in os.walk(mydir):
for file in files:
if (file.endswith('.xlsx') or file.endswith('.xls') or file.endswith('.XLS')):
filelist.append(os.path.join(path, file))
number_of_files=len(filelist)
print(filelist)
# Read all excel files and save to dataframe (df[0] - df[x]),
# x is the number of excel files that have been read - 1
df=[]
for i in range(number_of_files):
try:
df.melt(pd.read_excel(r''+filelist[i]))
except:
print('Empty Excel File')
print(df)
RESULTS:
Empty Excel File
Empty Excel File
Empty Excel File
Empty Excel File
[]
How I could unpivot the data instead of "appending" the data in columns?
I want to unpivot all my files data to this dataframe format.
Dataframe:
Id 1
Id 2
Id 3
test ab
test cs
test cs
category 4
category 3
category 1
index 1
index 1
index 1
remove dr
remove as
remove ae
stocks 4
stocks 3
stocks 1
category a
category b
category v