1

I try to perform some process on all Dataframe I have by using for loop by nothing changes.

Codes:

import pandas as pd

a = pd.DataFrame({'a':[1,2,3,4,5,6,7],
                 'b':[8,9,0,1,2,3,4]})
b = pd.DataFrame({'c':[1,2,3,4,5,6,7],
                 'b':[8,9,0,1,2,3,4]})

li = [a,b]
for i in li:
    'df_{}'.format(i) = i.rename(columns={'b':'test'})

Both a and b outputs:

    a   b
0   1   8
1   2   9
2   3   0
3   4   1
4   5   2
5   6   3
6   7   4

    c   b
0   1   8
1   2   9
2   3   0
3   4   1
4   5   2
5   6   3
6   7   4

Expected output:

    a   test
0   1   8
1   2   9
2   3   0
3   4   1
4   5   2
5   6   3
6   7   4

    c   test
0   1   8
1   2   9
2   3   0
3   4   1
4   5   2
5   6   3
6   7   4

can anyone point out what is wrong here? I try to use it on other dataset but nothing changes and I do not understand why. Please help.

Btw, I am wondering whether if I can make a different name for it like. above edited?

Anonymous
  • 477
  • 3
  • 12

3 Answers3

3

Use inplace=True in rename:

for i in li:
    i.rename(columns={'b':'test'}, inplace=True) # Without assignment

Output:

a.head()
   a  test
0  1     8
1  2     9
2  3     0
3  4     1
4  5     2

b.head()
   c  test
0  1     8
1  2     9
2  3     0
3  4     1
4  5     2
Chris
  • 29,127
  • 3
  • 28
  • 51
  • Thank you but I made some changes and wondering you can assist me on it? pls – Anonymous Jan 20 '20 at 02:18
  • @Anonymous Making dynamic variable name is simply a **bad** approach. Many good workaround can be found at [How do i create a variable number of variables](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Chris Jan 20 '20 at 02:20
  • Thanks and I try to use globals() but it wont work. – Anonymous Jan 20 '20 at 02:26
0

rename creates a new instance.

instead try:

for i in li:
    i = i.rename(columns={'b':'test'}, inplace=True)
voglster
  • 783
  • 6
  • 12
0

(EDIT):

It looks like the columns you're transforming are getting superpositioed on the copied object. The following workaround should solve this:

for i in li:
    f'df_{i}' = i.copy(deep=True)
    f'df_{i}' = f'df_{i}'.rename(columns={'b':'test'})
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69