3

Here I am trying to concat dataframe A and B with C using a for loop.

data = [['Alex',10],['Bob',12],['Clarke',13]]

A = pd.Dataframe(data, columns=['Name','Age'])
B = pd.Dataframe(data, columns=['Name','Age'])
C = pd.Dataframe(data, columns=['Name','Age'])

A.columns  ---> Index(['Name', 'Age'], dtype='object')
B.columns  ---> Index(['Name', 'Age'], dtype='object')
C.columns  ---> Index(['Name', 'Age'], dtype='object')

for df in [A, B]:
    df = pd.concat([df, C], axis=1)

A.columns  ---> Index(['Name', 'Age'], dtype='object')
B.columns  ---> Index(['Name', 'Age'], dtype='object')
df.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')

Why its not concatenating C with original A, B dataframes. Why it is creating a new df Dataframe?

I want after for loop:

A.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')

1 Answer 1

2

You're using Python mappings of names to object differently than how they work (you might be confused by other languages' references).

When you use

for df in [A, B]:
    df = pd.concat([df, C], axis=1)

Then df on the right side means "the object mapped to by the name df" (that is, A then B). df on the left side is just the name df. Your loop is therefore not modifying the original objects at all.


You can use

A, B = pd.concat([A, C], axis=1), pd.concat([B, C], axis=1)

If you really must use a loop, you can use a dict. First place the object there,

dfs = {'A': A, 'B': B}

then refer to them only through the dict:

for k, v in dfs.items():
    dfs[k] = pd.concat([v, C], axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, a loop probably isn't ideal here. I'd add two suggestions to Ami's answer: First, you could use a list instead of a dict (change to dfs = [A,B] and for k, v in enumerate(dfs)). Second, you could also do it as a comprehension instead of a for loop. As a list comprehension this would be dfs = [pd.concat([x, C], axis = 1) for x in [A,B]]

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.