For loop to update multiple dataframes

Question:

I wrote a few for loops and they all have the same issue: they do the correct operation, but do not overwrite the desired result in the old pandas dataframe. Here is what it looks like.

Example data:

       TimeStamp [µs] Source Channel Label Value [pV]
0              402600                  F10          0
1              402700                  F10          0
2              402800                  F10          0
3              402900                  F10          0
4              403000                  F10    -149012
              ...                  ...        ...
845142      299700800                  G10          0
845143      299700900                  G10          0
845144      299701000                  G10          0
845145      299701100                  G10          0
845146      299701200                  G10          0

In one of my loops I want to change the column names of all of my data sets. To do so, I did the following:

  1. Make a list of all dataframes I want to loop over:
list_dataset = [week6_233C, week6_233R, week6_233V, week6_401C, week6_401R, week6_401V, week5_233C, week5_233R, week5_233V]
  1. Write for loop to change the column names:
for i in list_dataset:
    i = i.rename(columns={'TimeStamp [µs]': 'Time', 'Source Channel Label': 'Electrode', 
                                            'Value [pV]': 'pV'})
    print(i)

This loop does print me the desired result:

              Time Electrode       pV
0          1906200        F7        0
1          1906300        F7        0
2          1906400        F7        0
3          1906500        F7  -149012
4          1906600        F7  -149012
           ...       ...      ...
1621073  300018100        G8        0
1621074  300018200        G8        0
1621075  300018300        G8        0
1621076  300018400        G8        0
1621077  300018500        G8        0

But information is not updated in the original data frame (still looks like the first example data above). How do I fix this? I thought by setting i = i.rename(columns=...) that it would be the same as writing week6_233C = week6_233C.rename(columns=...), however, the latter does update the original dataframe, whereas in the loop it does not..

Asked By: Celine Serry

||

Answers:

Your i variable is local to the loop. It does not modify the original object and once the next iteration happens it is overwritten.

You need to rename in place:

for i in list_dataset:
    i.rename(columns={'TimeStamp [µs]': 'Time',
                      'Source Channel Label': 'Electrode',
                      'Value [pV]': 'pV'},

             inplace=True)

Of assign back the mapped values:

for i in list_dataset:
    i.columns = i.rename(columns={'TimeStamp [µs]': 'Time',
                                  'Source Channel Label': 'Electrode',
                                  'Value [pV]': 'pV'},

                         ).columns
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.