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?

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but I made some changes and wondering you can assist me on it? pls
@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
Thanks and I try to use globals() but it wont work.
0

rename creates a new instance.

instead try:

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

1 Comment

Thank you but I made some changes and wondering you can assist me on it? pls
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'})

2 Comments

Thank you but I made some changes and wondering you can assist me on it? pls
Responded, take a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.