How to change multiple column headers during for-loop which append columns with .globe()?

Viewed 96

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!

1 Answers

The issue here is on your df.rename function. By default this function returns the renamed dataframe and needs to be assigned to a new variable. You have two options:

  1. Replace the current dataframe with
df = df.rename(columns={"cases7_lk": "cases7_lk_" + base})
  1. Use add the parameter inplace=True so it updates the current dataframe
df.rename(columns={"cases7_lk": "cases7_lk_" + base},
       inplace=True)
Related