I tried to append a specific column from various .csv-files with exact the same column headers and structure. Appending worked well so far, but I want to change the column headers to know which one is which date. I tried it on this way within the for-loop:
path = r'C:\Users\....\Daten'
all_files = glob.glob(path + "/*.csv")
#create empty list
li = []
# append future index to list
df0 = pd.read_csv(r'C:\ .... \Daten\RKI_Corona_Landkreise_20210320.csv')
li.append(df0["RS"])
# append future index to list
for filename in all_files:
df = pd.read_csv(filename)
# create String from filename and split it to the date
base = os.path.splitext(os.path.basename(filename))[0]
base = base.split('_')
base = base[3]
df.rename(columns={"cases7_lk": "cases7_lk_" + base})
li.append(df["cases7_lk"])
incidences_complete = pd.concat(li, axis=1)
incidences_complete = incidences_complete.set_index("RS")
incidences_complete
Output still looks like this: https://i.stack.imgur.com/BKNKs.png
I don't find the exact issue, but isolated the problem to the renaming of the DataFrame. Outside the for-loop it works fine, but inside the for-loop nothing happen. I also tried it with a new string variable combining old column header with base before renaming. Also with other columns and very simple dictionaries while appending it didn't worked either.
I hope you can help me. Thanks!