I have a seemingly simple piece of code but somehow it is not working. The goal of the code is to find all pickle data in a folder, load the first one in a for loop as a pandas dataframe which is named under a variable which did not exist before, if the variable exists, it should load the remaining pickle files as pandas and append them to the newly created pandas dataframe from the first loop:
import pandas as pd
import os
# Creating the first Dataframe using dictionary
df1 = pd.DataFrame({"a":[1, 2, 3, 4],
"b":[5, 6, 7, 8]})
# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
"b":[5, 6, 7]})
df1.append(df2)
works fine printing:
a b
0 1 5
1 2 6
2 3 7
3 4 8
0 1 5
1 2 6
2 3 7
However when I try to append the dataframes from my stored pickle files in a for loop it does not print an error but it only works for the first dataframe:
df1.to_pickle("DF1.pkl")
df2.to_pickle("DF2.pkl")
files = [f for f in os.listdir('.') if os.path.isfile(f)]
#The line above should produce the line below
files=["DF1.pkl", "DF2.pkl"]
for i in files:
if ".pkl" in i:
if "ALL_DATA" not in globals():
ALL_DATA=pd.read_pickle(i)
else:
ALL_DATA.append(pd.read_pickle(i))
which only prints:
a b
0 1 5
1 2 6
2 3 7
3 4 8
Who can help me clarify?